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

StackOverflow https://stackoverflow.com/questions/21537121

  •  06-10-2022
  •  | 
  •  

문제

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?

도움이 되었습니까?

해결책

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')
  }
);

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top