Categories
JavaScript Answers

How to wait some asynchronous tasks complete in JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *