Series independent mono-tasking

suggest change

async.series(tasks, afterTasksCallback) runs tasks one after another. If a task fails, async stops and calls the main callback.

When every task succeeds, async calls that callback with null for the error and an array of results.

function shortTimeFunction(callback) {
  setTimeout(function() {
    callback(null, 'resultOfShortTime');
  }, 200);
}

function mediumTimeFunction(callback) {
  setTimeout(function() {
    callback(null, 'resultOfMediumTime');
  }, 500);
}

function longTimeFunction(callback) {
  setTimeout(function() {
    callback(null, 'resultOfLongTime');
  }, 1000);
}

async.series([
    mediumTimeFunction,
    shortTimeFunction,
    longTimeFunction
  ],
  function(err, results) {
    if (err) {
      return console.error(err);
    }

    console.log(results);
  });

Result : ["resultOfMediumTime", "resultOfShortTime", "resultOfLongTime"].

Call async.series() with an object

You can pass an object instead of the tasks array. The results are then an object with the same keys as the tasks.

async.series({
    short: shortTimeFunction,
    medium: mediumTimeFunction,
    long: longTimeFunction
  },
  function(err, results) {
    if (err) {
      return console.error(err);
    }

    console.log(results);
  });

Result : {short: "resultOfShortTime", medium: "resultOfMediumTime", long: "resultOfLongTime"}.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents