简体   繁体   中英

How to make sure that a foreach loop running async calls finished?

I have a foreach loop, where I call an async function. How can I make sure that all the async functions called the specified callback function, and after that, run something?

Keep a counter. Example:

const table = [1, 2, 3];
const counter = 0;

const done = () => {
     console.log('foreach is done');   
}

table.forEach((el) => {
   doSomeAsync((err, result) => {
      counter++;
      if (counter === 3) {
         done();
      }
   });
});

As the other answer says, you can use the async package which is really good. But for the sake of it I recommend using Promises and use the Vanila Promise.all(). Example:

const table = [1, 2, 3];

Promise.all(table.map((el) => {
   return new Promise((resolve, reject) => {
       doSomeAsync((err, result) => {
            return err ? reject(err) : resolve(result); 
       });  
   });
}))
.then((result) => {
     // when all calls are resolved
})
.catch((error) => {
     // if one call encounters an error
});

You can use Async library for this. It has various useful utility functions.

There is a Queue function in it which can be used to execute a set of tasks and you get a callback when all tasks are executed where you can do whatever you want. You can also control the concurrency of your queue(how many tasks are executed at a time in parallel).

Here is a sample code-

// create a queue object with concurrency 2
var q = async.queue(function(task, callback) {
    console.log('hello ' + task.name);
    callback();
}, 2);

// The callback function which is called after all tasks are processed
q.drain = function() {
    console.log('all tasks have been processed');
};

// add some tasks to the queue
q.push({name: 'foo'}, function(err) {
    console.log('finished processing foo');
});
q.push({name: 'bar'}, function (err) {
    console.log('finished processing bar');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM