Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
741 views
in Technique[技术] by (71.8m points)

javascript - Is a call to Node.js' async.parallel() synchronous?

I am taking a look at Node.js' async module to solve an issue. I have implemented 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 the Trulu log call will never be called before the call to async.parallel is finished? In other words, are all the functions and the final callback called before the Trulu log is called for sure?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Short answer: No they are asynchronous.

Long answer:

As soon as you do anything asynchronous, inside the parralel callbacks, you can expect Trulu to get called immediatly after. When you call cb, it announce that the function returned. When all function have returned it will call the last callback.

So for that reason, if you do a setTimeout without a time and each functions are returning without anything asynchrone, they might behave as synchronous because Javascript only runs one unit at a time. And since they are probably started in order, it will look as if it run synchronously.

Take this as an example:

var looping = true;

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

while(looping) {
   ...
}

This code will never leave as setTimeout will never get triggered. In async.parralel, I'm almost certain that each callbacks are going to get executed after the current unit is finished running. Which means, that you'll always have your console.log("Trulu") executed before any of the callbacks inside async.parralel.

Note

If you're looking for to do synchronous things, you shouldn't use a module called async. It's quite obvious that it's going to be asynchronous.

That said, I feel that there is a big misunderstanding with the term async. Asynchronous isn't a synonym for parralelism or having things simultaneous. Asynchrone, just means "without order in time". It could be simultaneous but it could be also sequential. To run asynchronous code on Javascript, you have to rely on an event loop.

The event loop can only process one event at a time, setTimeout is just putting the event on the loop. As soon as an event handler leaves, it gets to the next event and so on.

In your case, since you're not calling cb inside an event handler, they will be called synchronously as they never leave the current event.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...