node.js async.series 是它应该如何工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15969082/
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
node.js async.series is that how it is supposed to work?
提问by voicestreams
var async = require('async');
function callbackhandler(err, results) {
console.log('It came back with this ' + results);
}
function takes5Seconds(callback) {
console.log('Starting 5 second task');
setTimeout( function() {
console.log('Just finshed 5 seconds');
callback(null, 'five');
}, 5000);
}
function takes2Seconds(callback) {
console.log('Starting 2 second task');
setTimeout( function() {
console.log('Just finshed 2 seconds');
callback(null, 'two');
}, 2000);
}
async.series([takes2Seconds(callbackhandler),
takes5Seconds(callbackhandler)], function(err, results){
console.log('Result of the whole run is ' + results);
})
The output looks like below :
输出如下所示:
Starting 2 second task
Starting 5 second task
Just finshed 2 seconds
It came back with this two
Just finshed 5 seconds
It came back with this five
I was expecting the takes2Second function to finish completely before the takes5Second starts. Is that how it is supposed to work. Please let me know. And the final function never runs. Thanks.
我期待takes2Second 函数在takes5Second 开始之前完全完成。这是它应该如何工作。请告诉我。最后一个函数永远不会运行。谢谢。
回答by James Allardice
Not quite. You are executing the functions immediately (as soon as the array is evaluated), which is why they appear to start at the same time.
不完全的。您正在立即执行这些函数(一旦对数组求值),这就是它们似乎同时启动的原因。
The callback passed to each of the functions to be executed is internal to the async library. You execute it once your function has completed, passing an error and/or a value. You don't need to define that function yourself.
传递给要执行的每个函数的回调是异步库内部的。一旦你的函数完成,你就执行它,传递一个错误和/或一个值。您不需要自己定义该函数。
The final function never runs in your case because the callback function that async needs you to invoke to move on to the next function in the series never actually gets executed (only your callbackHandlerfunction gets executed).
最后一个函数永远不会在您的情况下运行,因为 async 需要您调用以继续执行系列中的下一个函数的回调函数实际上从未被执行(只有您的callbackHandler函数被执行)。
Try this instead:
试试这个:
async.series([
takes2Seconds,
takes5seconds
], function (err, results) {
// Here, results is an array of the value from each function
console.log(results); // outputs: ['two', 'five']
});
回答by Noah
James gave you a good overview of async.series. Note that you can setup anonymous functions in the series array and then call your actual functions with parameters
James 为您提供了一个很好的概述async.series。请注意,您可以在系列数组中设置匿名函数,然后使用参数调用您的实际函数
var async = require('async')
var param1 = 'foobar'
function withParams(param1, callback) {
console.log('withParams function called')
console.log(param1)
callback()
}
function withoutParams(callback) {
console.log('withoutParams function called')
callback()
}
async.series([
function(callback) {
withParams(param1, callback)
},
withoutParams
], function(err) {
console.log('all functions complete')
})
回答by Noah
My preferred way to create the async series is using operational array as follow;
我创建异步系列的首选方法是使用操作数组,如下所示;
var async = require('async'),
operations = [];
operations.push(takes2Seconds);
operations.push(takes5seconds);
async.series(operations, function (err, results) {
// results[1]
// results[2]
});
function takes2Seconds(callback) {
callback(null, results);
}
function takes5seconds(callback) {
callback(null, results);
}
回答by AliPrf
async.series
([
function (callback)
{
response=wsCall.post(user,url,method,response);
console.log("one");
callback();
}
,
function (callback)
{
console.log("two");
//console.log(response);
callback();
}
]
,
function(err)
{
console.log('Both a and b are saved now');
console.log(response);
});
回答by vineet
In async.series,all the functions are executed in series and the consolidated outputs of each function is passed to the final callback. e.g
在 中async.series,所有函数都串行执行,并且每个函数的合并输出传递给最终回调。例如
var async = require('async');
async.series([
function (callback) {
console.log('First Execute..');
callback(null, 'userPersonalData');
},
function (callback) {
console.log('Second Execute.. ');
callback(null, 'userDependentData');
}
],
function (err, result) {
console.log(result);
});
Output:
输出:
First Execute..
Second Execute..
['userPersonalData','userDependentData'] //result

