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);
})
出力は以下のようになります。node.js async.seriesは、それがどのように動作するのでしょうか?
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
私はtakes5Secondが始まる前に完全に終了するtakes2Second機能を期待していました。 それはそれが働く方法ですか?私にお知らせください。 最後の機能は決して実行されません。ありがとう。
ジェームス、何が起こっているのあなたの明確な説明に感謝。私がこれらの変更を提案した後で動作します。これらの関数に引数を渡すことは可能でしょうか? – voicestreams
ドキュメントをさらに読むと、async.applyを使用して関数に引数を渡す必要があるようです。 – voicestreams