Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will async.parallel still call the final callback after all tasks are done if any of them gets error?

var async = require('async');
async.parallel([
  function(cb) {
    cb(true);
  },
  function(cb) {
    cb(null, true);
  }], 
  function(error, results) {
  }
);

In the code, if the first task runs cb(true) before the second tasks, will the second tasks still run? and If so, after it is done, will the main callback still be called?

like image 974
Joe C Avatar asked Feb 03 '14 20:02

Joe C


People also ask

How does async parallel work?

Asynchronous operations in parallel The first argument to async. parallel() is a collection of the asynchronous functions to run (an array, object or other iterable). Each function is passed a callback(err, result) which it must call on completion with an error err (which can be null ) and an optional results value.

How does async eachSeries work?

async. eachSeries() applies an asynchronous function to each item in an array in series. For example, say you have a list of users, each of which needs to post its profile data to remote server log. Order matters in this case because the users in your array are sorted.

What is the difference between async waterfall and async series?

The async. waterfall allows each function to pass on its results to the next function, while async. series passes all the task's results to the final callback.

What are the two arguments that async queue takes?

Below are the two arguments that async. queue takes as input: Task Function. Concurrency Value.


2 Answers

yes, second task is called (because tasks are expected to be async and exit immediately). async.parallel callback is called with error from first failed task

like image 40
Andrey Sidorov Avatar answered Oct 22 '22 20:10

Andrey Sidorov


The async.parallel executes all functions in parallel. If any of the functions pass an error to its callback (callback first parameter is not null), the main callback is immediately called with the value of the error. All functions will be executed though.

With the following code your execution will be as follows 1, 3, 2, 2.1:

var async = require('async');
async.parallel([
  function(cb) {
    console.info('1')
    cb(true);
  },
  function(cb) {
    console.info('2')
    cb(null, true);
  },
  function(cb) {
    console.info('2.1')
    cb(null, true);
  }], 
  function(error, results) {
    console.info('3')
  }
);
like image 78
Tom Avatar answered Oct 22 '22 21:10

Tom