Is calling Node.js' async.parallel () synchronous?

I am considering a Node.js' module asyncto solve a problem. I performed a little test:

var async = require("async");

function print(val) {
    console.log(val);
}

async.parallel([
    function(cb){ print(1); cb(null); },
    function(cb){ print(2); cb(null); },
    function(cb){ print(3); cb(null); },
    function(cb){ print(4); cb(null); },
    function(cb){ print(5); cb(null); }
],
    function(err) {
        if ( err ) {
            console.error(err);
            return;
        }
        console.log("Done!");
    }
);

console.log("Trulu");

Can I be sure that Trulu log call will never be called until the call is completed async.parallel? In other words, all functions and the final callback are called before the Trulu log is called?

+1
source share
3 answers

Can I be sure that the Trulu log call will never be called until the async.parallel call is completed?

The challenge itself, yes.

In other words, all functions

I think so, although this is not mentioned in the documents. I would, however, not expect any changes, as this could break existing code.

, , Trulu ?

. , , Trulu. , async. , async.js setImmediate , ( , ) "", :

,

, ( Trulu), , A + compliant Promises.

+3

: , .

:

- , parralel, , Trulu . cb, , . , .

, setTimeout , - , , Javascript . , , , , .

:

var looping = true;

setTimeout(function () {looping = false;}, 1000);

while(looping) {
   ...
}

, setTimeout . async.parralel , . , console.log("Trulu"), async.parralel.

, async. , .

, , async. . , " ". , . Javascript, .

, setTimeout . , ..

, cb , , event.

+3

async , async series. , , , , ( , -, async.js ):

async.series([
  parallel,
  function(callBack){ console.log("Trulu"); callBack(null); }
]);

function parallel(callBack){

  async.parallel([
    function(cb){ print(1); cb(null); },
    function(cb){ print(2); cb(null); },
    function(cb){ print(3); cb(null); },
    function(cb){ print(4); cb(null); },
    function(cb){ print(5); cb(null); }
  ],
    function(err) {
        if ( err ) {
            console.error(err);
            return;
        }
        console.log("Done!");
        callBack(null);
    }
  );
}

, , javascript , . , , , , , , !

Another simpler solution would be to create the last call you want to make after the built-in callback async.parallel:

async.parallel([
  function(cb){ print(1); cb(null); },
  function(cb){ print(2); cb(null); },
  function(cb){ print(3); cb(null); },
  function(cb){ print(4); cb(null); },
  function(cb){ print(5); cb(null); }
],
  function(err) {          
      if ( err ) {
          console.error(err);
          return;
      }
      console.log("Done!");          
      console.log("Trulu"); // Executes after all your "parallel calls" are done
  }
);
+1
source

All Articles