async for loop nodejs for sending chunks to database

Viewed 943

I need loop into an object array sending chunks to a server to store in database. like this simplified example:

(async function () {
    var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
    var i, j, block, chunk = 3;
    for (i=0, j=array.length; i<j; i+=chunk) {
        block = array.slice(i,i+chunk);
        console.dir(block);
        //Some async promise to do the magic
    }
})();

The problem is that in nodejs, traditional for loop doesn't works asynchronous. This other loop works fine:

(async function () {
    for (const element of rows) {
        //Some async promise
    }
})();

How could I convert the first loop in chunks like the second one that works? are any other better sintax to do the same?

3 Answers

You just need to make your variables block-scoped to overcome closure problem

(async function () {
    var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
    for (let i=0, j=array.length, block, chunk = 3; i<j; i+=chunk) {
            block = array.slice(i,i+chunk);
            console.dir(block);
            //Some async promise to do the magic
    }
})();
(async function () {
    var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
    var i, j, chunk = 3;
    for (i=0, j=array.length; i<j; i+=chunk) {

        //Now block variable is block-scope
        let block = array.slice(i,i+chunk);

        console.dir(block);
        //Some async promise to do the magic
    }
})();

You can use await and for .. of to write to the database in sequence, assuming your database write function returns a promise.

You could use a standard for loop if you wish too.

const rows = [...Array(5).keys()].map(n => `entry${n+1}`);

function writeToDatabaseMock() {
    return new Promise(resolve => setTimeout(resolve, 1000));
}

(async function () {
    for (const [index, element] of rows.entries()) {
        console.log(`Writing element #${index+1} to database:`, element);
        await writeToDatabaseMock();
    }
})();

Related