Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js async.series is that how it is supposed to work?

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.

like image 275
voicestreams Avatar asked Apr 12 '13 10:04

voicestreams


People also ask

How does async series work?

series: The async. series method runs the functions in the tasks collection in series. Each one runs once the previous function has completed. If any functions in the series pass an error to its callback, no more functions are executed.

What is async series in node JS?

Async. series takes the collection of asynchronous functions and optional callback method. When all the tasks complete the execution, then the final callback will be called and return the results to the server. This result variable will hold the array of the item with the company, job, application, and licence object.

How does async work in node JS?

readFile (an async method provided by Node) reads the file and when it finishes it calls the callback function with an error or the file content. In the meantime the program can continue code execution. An async callback may be called when an event happens or when a task completes.

What is async series and async parallel in node JS?

async. series invokes your functions serially (waiting for each preceding one to finish before starting next). async. parallel will launch them all simultaneously (or whatever passes for simultaneous in one-thread land, anyway).


1 Answers

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 callbackHandler function gets executed).

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'] }); 
like image 151
James Allardice Avatar answered Sep 22 '22 06:09

James Allardice