Nodejs 异步系列 - 将参数传递给下一个回调
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22424592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Nodejs async series - pass arguments to next callback
提问by clarkk
When you use async module, how can you then pass arguments from the previous callback to the next?
当您使用 async 模块时,如何将参数从上一个回调传递到下一个?
Here is an example from the docs on github
这是 github 上的文档中的一个示例
async.series({
one: function(callback){
setTimeout(function(){
callback(null, 1);
}, 200);
},
two: function(callback){
setTimeout(function(){
callback(null, 2);
}, 100);
}
},
function(err, results) {
// results is now equal to: {one: 1, two: 2}
});
回答by AlexMA
You can chain together asynchronous functions with the async module's waterfallfunction. This allows you to say, "first do x, then pass the results to function y, and pass the results of that to z." Copied from the [docs][1]:
您可以将异步函数与 async 模块的waterfall函数链接在一起。这允许您说,“首先执行 x,然后将结果传递给函数 y,然后将其结果传递给 z。” 复制自 [docs][1]:
async.waterfall([
function(callback){
callback(null, 'one', 'two');
},
function(arg1, arg2, callback){
// arg1 now equals 'one' and arg2 now equals 'two'
callback(null, 'three');
},
function(arg1, callback){
// arg1 now equals 'three'
callback(null, 'done');
}
], function (err, result) {
// result now equals 'done'
});
You don't strictly need the async module to accomplish this; this function is designed to make the code easier to read. If you don't want to use the async module, you can always just use traditional callbacks.
您并不严格需要 async 模块来完成此操作;此功能旨在使代码更易于阅读。如果你不想使用 async 模块,你总是可以使用传统的回调。
回答by Mike Graf
Another option is to use async.auto. With async auto you can specify the dependency data for a task and async will begin to run it when able to. There is a good example in the README.md, but here is roughly your series from above:
另一种选择是使用 async.auto。使用 async auto,您可以指定任务的依赖数据,并且 async 将在可以时开始运行它。README.md 中有一个很好的例子,但这里大致是你上面的系列:
async.auto({
one: function(callback){
setTimeout(function(){
callback(null, 1);
}, 200);
},
// If two does not depend on one, then you can remove the 'one' string
// from the array and they will run asynchronously (good for "parallel" IO tasks)
two: ['one', function(callback, results){
setTimeout(function(){
callback(null, 2);
}, 100);
}],
// Final depends on both 'one' and 'two' being completed and their results
final: ['one', 'two',function(err, results) {
// results is now equal to: {one: 1, two: 2}
}];
});
回答by user1744917
I spent quite a bit of time solving this issue because I encountered similar situation. I tried both async.seriesand async.waterfall.
我花了很多时间解决这个问题,因为我遇到了类似的情况。我尝试了async.series和async.waterfall。
async.series:Used a variable to share across the callback functions. I am not sure whether this is the best way to do it. I must give credit to Sebastianfor his wonderful article about async.
async.series:使用一个变量在回调函数之间共享。我不确定这是否是最好的方法。我必须感谢塞巴斯蒂安 ( Sebastian)关于async.
var async1 = require('async');
exports.asyncSeries1 = function (req, res, callback) {
var sharedData = "Data from : ";
async1.series([
// First function
function(callback) {
sharedData = "First Callback";
callback();
},
// Second function
function(callback){
console.log(sharedData);
sharedData = "Second Callback";
callback();
}
],
// Final callback
function(err) {
console.log(sharedData);
if (err) {
callback();
}
callback();
}
);
};
async.waterfall: I tried using another callback function using async.apply. Here is the piece of code that helped me solve the problem.
`
async.waterfall:我尝试使用另一个回调函数使用async.apply. 这是帮助我解决问题的一段代码。`
var async2 = require('async')
exports.asyncWaterfall1 = function (arg1, arg2, cb) {
async2.waterfall([
// async.apply
async2.apply(assignVariables, arg1, arg2),
// First callback
function(arg1, arg2, callback){
console.log(arg1);
console.log(arg2);
arg1 = 5;
arg2 = 6;
callback(null, arg1, arg2);
},
// Second callback
function(arg1, arg2, callback){
// arg1 now equals 'one' and arg2 now equals 'two'
console.log(arg1);
console.log(arg2);
arg1 = 7;
arg2 = 8;
callback(null, arg1, arg2);
}
],
function (err, arg1, arg2) {
console.log(arg1);
console.log(arg2);
});
};
// Method to assign variables
function assignVariables(arg1, arg2, callback) {
console.log(arg1);
console.log(arg2);
arg1 = 3;
arg2 = 4;
callback(null, arg1, arg2);
};
PS: Credit.
PS:信用。

