1

First analogue php - function sleep in js:

 function sleep(seconds) {
    var startTime = new Date().getTime();
    while (new Date().getTime() < startTime + seconds*1000);
  }

Then my example :

var async = require('async');

var a = function(callback)
{   
    sleep(10)
    console.log("Hello ");
    callback(null,'vals1')
   // return 'prop' 
};
var b = function(callback)
{  
    //I want get  return 'prop'  from a() its posible ?
    sleep(30)
    console.log("World");
     callback(null,"vals2")
};

var c = function(callback)
{  
    //I want get  return 'prop'  from a() its posible ?
    sleep(2)
    console.log(" and U");
     callback(null,"val3")
};
async.parallel(
[
    a,b,c
],function(err,vals){

    //console.log(err)//undefined
    console.log(vals) //[val1,vals2]
    console.log('finals')
}
    );

series(tasks, [callback])

Run the functions in the tasks array in series, each one running once the previous function has completed. If any functions in the series pass an error to its callback, no more functions are run, and callback is immediately called with the value of the error. Otherwise, callback receives an array of results when tasks have completed.

parallel(tasks, [callback])

Run the tasks array of functions in parallel, without waiting until the previous function has completed. If any of the functions pass an error to its callback, the main callback is immediately called with the value of the error. Once the tasks have completed, the results are passed to the final callback as an array.

My result :

break about 10sec 
Hello 

break about 30sec 
World 

and small break
and U 

I expected to perform the functions in the following order :

output immediately 
and U 

break about 10sec 
Hello 

break about 30sec 
World 

If even replace your example on

async.series()

the result will be the same

Leonid Beschastny
  • 50,364
  • 10
  • 118
  • 122
Michael Phelps
  • 3,451
  • 7
  • 36
  • 64
  • It's a really bad idea to implement blocking sleep in node.js. – Leonid Beschastny Aug 28 '14 at 10:52
  • 1
    If you want to model an asynchronous function call (e.g. db query), use [build-in asynchronous `setTimeout`](http://nodejs.org/api/timers.html#timers_settimeout_callback_delay_arg) instead. – Leonid Beschastny Aug 28 '14 at 10:54
  • 2
    possible duplicate of [Async.js - Is parallel really parallel?](http://stackoverflow.com/questions/19023977/async-js-is-parallel-really-parallel) – Leonid Beschastny Aug 28 '14 at 10:56
  • Possible duplicate of [difference between async.series and async.parallel](http://stackoverflow.com/questions/17853105/difference-between-async-series-and-async-parallel) – sidgate Sep 14 '16 at 18:47

1 Answers1

1

To answer your question: parallel run these functions in parallel and series run one after another.

You code doesn't work as expected because nodejs offers only one process to all functions and your functions do a 'busy wait'. Replace them with setTimeout() and you get the expected results...

CFrei
  • 3,552
  • 1
  • 15
  • 29