How can I add a setTimeOut() to run a function and insert data multiple times?

A few days ago I did a project and I had some problems, which where solved in this question, let me try to resume it.

I need to insert multiple objects into a DB in SQLServer, for that, I did a function that loops another function, which opens a connection, inserts and closes the connection, then, repeats it over and over again.

It worked fine, till today that was tested in a collegue PC, in the server of the job, I get this error:

Error: Requests can only be made in the LoggedIn state, not the LoggedInSendingInitialSql state

Here’s the code we tested (the same in my last question), it works in my PC, but not in the other:

var config = {
   ...
};

function insertOffice(index) {
    var connection = new Connection(config);
    connection.on("connect", function (err) {
        console.log("Successful connection");
    });
    connection.connect();
    
    let url = `https://api.openweathermap.org/data/2.5/weather?lat=${offices[index].latjson}&lon=${offices[index].lonjson}&appid=${api_key}&units=metric&lang=sp`;
    fetch(url)
        .then((response) => { return response.json(); })
        .then(function (data) {
            var myObject = {
                Id_Oficina: offices[index].IdOficina,
                ...
            };

            const request = new Request(
                "EXEC USP_BI_CSL_insert_reg_RegistroTemperaturaXidOdicina @IdOficina, ...",
                function (err) {
                    if (err) {
                        console.log("Couldnt insert data (" + index + "), " + err);
                    } else {
                        console.log("Data with ID: " + myObject.Id_Oficina +" inserted succesfully(" + index + ").")
                    }
                }
            );
            request.addParameter("IdOficina", TYPES.SmallInt, myObject.Id_Oficina);
            ...
            request.on("row", function (columns) {
                columns.forEach(function (column) {
                    if (column.value === null) {
                        console.log("NULL");
                    } else {
                        console.log("Product id of inserted item is " + column.value);
                    }
                });
            });
            request.on("requestCompleted", function () {
                connection.close();
            });
            connection.execSql(request);
        });
}

function functionLooper() {
    for (let i = 0; i < offices.length; i++) {
        let response = insertOffice(i);
    }
}

functionLooper();

So, I thought it would be a good idea to use a setTimeOut, to:

  1. Run functionLooper().
  2. Open connection, insert and close.
  3. Wait a few seconds.
  4. Repeat.

So, I changed to this:

setTimeout(functionLooper, 2000);

function functionLooper() {
    for (let i = 0; i < offices.length; i++) {
        let response = insertOffice(i);
    }
}

It works, but, as you can see, only waits when I first run it, so tried to make a function that runs setTimeout(functionLooper, 2000); like functionLooper() does, but it didn’t work either.

function TimerLooper() {
    for (let i = 0; i < offices.length; i++) {
        setTimeout(functionLooper, 500);   
    }
    
}

function functionLooper() {
    for (let i = 0; i < offices.length; i++) {
        let response = insertOffice(i);
    }
}

TimerLooper();

This shows me this error:

Error: Validation failed for parameter ‘Descripcion’. No collation was set by the server for the current connection.
file:///…:/…/…/node_modules/node-fetch/src/index.js:95
reject(new FetchError(request to ${request.url} failed, reason: ${error.message}, ‘system’, error));
^ FetchError: request to https://api.openweathermap.org/data/2.5/weather?lat=XX&lon=XX&appid=XX&units=metric&lang=sp failed, reason: connect ETIMEDOUT X.X.X.X:X

So, I have some questions

  1. How can I use properly setTimeOut? I did this function based on what I watch here in SO, but I just can’t get it and I don’t know what I’m doing wrong.
  2. Why it works in my PC and the other don’t? Do we have to change some kind of config or something?
  3. Using setTimeOut, is the correct way to solve this problem? if not, what would you sugegst me?

If any extra explanation needed, let me know and I’ll edit my question.