我可以在 async.parallel 中使用 async.waterfall 吗?
can I use async.waterfall inside async.parallel?
我想调用两个函数并并行获取结果,但其中一个函数的结果需要调整。所以函数结构是:
function test(cb) {
async.parallel({
func1: function foo(cb1) {
cb(null, {});
},
func2: function bar(cb2) {
async.waterfall([
function adapt1(next) {
//do something;
},
function adapt2(next) {
//do something;
}
], function handler(err, res) {
//do something.
})
}
}, function handler2(err, res) {
cb(null, {});
})
}
然而,它似乎永远挂在那里。 不确定我是否可以以这种方式使用异步....
当然可以!您必须确保首先以正确的顺序调用回调。例如,func1 应该调用cb1
而不是cb
。其次,您的瀑布根本不调用他们的回调。以这段代码为例。
'use strict';
let async = require('async');
function test(callback) {
async.parallel({
func1: function(cb) {
cb(null, { foo: 'bar' });
},
func2: function(cb) {
async.waterfall([
function(cb2) {
cb2(null, 'a');
},
function(prev, cb2) {
cb2(null, 'b');
}
], function(err, result) {
cb(err, result);
});
}
}, function(err, results) {
callback(err, results);
});
}
test(function(err, result) {
console.log('callback:', err, result);
});
输出:callback: null { func1: { foo: 'bar' }, func2: 'b' }
相关文章:
- Stubbing async.waterfall with Sinon.JS
- Async.waterfall 函数在保存解析对象后退出
- 如何将async.waterfall与现有的回调一起使用
- express js中的async.waterfall方法
- nest async.waterfall in async.forEachSeries
- 如何将嵌套_.each转换为同步方法?首选使用 Promise 或 Async.waterfall
- Async.waterfall 的本机回调函数
- res.end() 是否结束了 async.waterfall
- 方法在 async.waterfall 中不起作用
- 我可以在 async.parallel 中使用 async.waterfall 吗?
- 失去与 async.waterfall 的范围
- 如何组织 async.waterfall 调用的循环
- Node.js依赖的API请求调用使用async.waterfall,并将JSON响应与undercore.extend
- async.waterfall只返回函数数组(node js)的array[0]索引处的函数的结果集
- 为什么每个任务的计数's的参数在async.waterfall中应该相同
- 从async.waterfall分配全局变量
- async.waterfall和async.series之间有什么区别
- 如何使用jQuery Deferred功能而不是async.waterfall
- async.waterfall 无法按预期工作
- 异步.应用于async.waterfall内部