简体   繁体   English

使用节点异步并行回调

[英]using callback of node async parallel

I have few functions that can be executed in parallel. 我几乎没有可以并行执行的功能。 Using async.parallel() works fine, but it executes the main callback when an error is received. 使用async.parallel()可以正常工作,但是当收到错误时,它将执行主回调。

As the functions are independent, I still want results from others even though on has error. 由于功能是独立的,因此即使出现错误,我仍然希望其他结果。

Here's my current code. 这是我当前的代码。 Is there another async function for my need? 我是否需要另一个async功能?

var async=require('async');
async.parallel({
    one:function(callback)
    {
        callback(null,1);
    },
    two:function(callback)
    {
        callback("ERROR Occured");
    },
    three:function(callback)
    {
        callback(null,3);
    },
    four:function(callback)
    {
        callback(null,4);
    }
},function(err,result)
{
    console.log(err);
    console.log(result);
});

You can use reflectAll : 您可以使用reflectAll

var async = require('async');

async.parallel(async.reflectAll([
  function(callback) {
    callback(null,1);
  },
  function(callback) {
    callback("ERROR Occured");
  },
  function(callback) {
    callback(null,3);
  },
  function(callback) {
    callback(null,4);
  }
]), function(err,result) {
  console.log(err);
  console.log(result);
});

How this works is explained here : 这里是如何工作的:

Wraps the function in another function that always returns data even when it errors. 将函数包装在另一个总是返回数据的函数中,即使它出错。 The object returned has either the property error or value . 返回的对象具有属性errorvalue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM