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);
})
L'uscita si presenta come di seguito:node.js async.series è che come dovrebbe funzionare?
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
mi aspettavo la funzione takes2Second di finire completamente prima dell'inizio della takes5Second. È così che dovrebbe funzionare. Per favore mi faccia sapere. E la funzione finale non funziona mai. Grazie.
James, grazie per la vostra spiegazione chiara su quello che sta succedendo. Funziona dopo aver apportato tali modifiche come suggerito. È possibile passare argomenti a queste funzioni? – voicestreams
Su ulteriore lettura dei documenti, sembra che dovrei usare async.apply per passare argomenti a funzioni. – voicestreams