To wait some asynchronous tasks complete in JavaScript, we call the async.each
method.
For instance, we write
const async = require("async");
async.each(
["aaa", "bbb", "ccc"],
(name, callback) => {
conn.collection(name).drop(callback);
},
(err) => {
if (err) {
return console.log(err);
}
console.log("all dropped");
}
);
to call async.each
to with an input array.
The first callback calls some async function with each item in the array.
And the 2nd callback is an error handler callback.