Simplest way to wait some asynchronous tasks complete, in Javascript? Simplest way to wait some asynchronous tasks complete, in Javascript? mongoose mongoose

Simplest way to wait some asynchronous tasks complete, in Javascript?


Use Promises.

var mongoose = require('mongoose');mongoose.connect('your MongoDB connection string');var conn = mongoose.connection;var promises = ['aaa', 'bbb', 'ccc'].map(function(name) {  return new Promise(function(resolve, reject) {    var collection = conn.collection(name);    collection.drop(function(err) {      if (err) { return reject(err); }      console.log('dropped ' + name);      resolve();    });  });});Promise.all(promises).then(function() { console.log('all dropped)'); }).catch(console.error);

This drops each collection, printing “dropped” after each one, and then prints “all dropped” when complete. If an error occurs, it is displayed to stderr.


Previous answer (this pre-dates Node’s native support for Promises):

Use Q promises or Bluebird promises.

With Q:

var Q = require('q');var mongoose = require('mongoose');mongoose.connect('your MongoDB connection string');var conn = mongoose.connection;var promises = ['aaa','bbb','ccc'].map(function(name){    var collection = conn.collection(name);    return Q.ninvoke(collection, 'drop')      .then(function() { console.log('dropped ' + name); });});Q.all(promises).then(function() { console.log('all dropped'); }).fail(console.error);

With Bluebird:

var Promise = require('bluebird');var mongoose = Promise.promisifyAll(require('mongoose'));mongoose.connect('your MongoDB connection string');var conn = mongoose.connection;var promises = ['aaa', 'bbb', 'ccc'].map(function(name) {  return conn.collection(name).dropAsync().then(function() {    console.log('dropped ' + name);  });});Promise.all(promises).then(function() { console.log('all dropped'); }).error(console.error);


I see you are using mongoose so you are talking about server-side JavaScript. In that case I advice looking at async module and use async.parallel(...). You will find this module really helpful - it was developed to solve the problem you are struggling with. Your code may look like this

var async = require('async');var calls = [];['aaa','bbb','ccc'].forEach(function(name){    calls.push(function(callback) {        conn.collection(name).drop(function(err) {            if (err)                return callback(err);            console.log('dropped');            callback(null, name);        });    })});async.parallel(calls, function(err, result) {    /* this code will run after all calls finished the job or       when any of the calls passes an error */    if (err)        return console.log(err);    console.log(result);});


The way to do it is to pass the tasks a callback that updates a shared counter. When the shared counter reaches zero you know that all tasks have finished so you can continue with your normal flow.

var ntasks_left_to_go = 4;var callback = function(){    ntasks_left_to_go -= 1;    if(ntasks_left_to_go <= 0){         console.log('All tasks have completed. Do your stuff');    }}task1(callback);task2(callback);task3(callback);task4(callback);

Of course, there are many ways to make this kind of code more generic or reusable and any of the many async programing libraries out there should have at least one function to do this kind of thing.