Question

I'm trying to understand the behaviour of the async package in relation to parallelisation of code. From what I understand, it is helpful to structure code in order to avoid callback hell/soup, but is that the only advantage?

async.parallel([
    function(next) {
        apiCallTo3rdParty(next);
    },
    function(next) {
        apiCallTo3rdParty(next);
    },
    function(next) {
        apiCallTo3rdParty(next);
    }
], function(err, res) {
    // do something else with returned data
});

In the code above, all three api calls would be made without waiting for the other to complete. As per spec. The final callback would be called after the longest API call returns, as the rest would have completed prior.

However, if I changed the code so that it made a couple blocking operations, what would happen?:

async.parallel([
    function(next) {
        sleep(5);
    },
    function(next) {
        sleep(5);
    },
    function(next) {
        sleep(5);
    }
], function(err, res) {
    // do something else with returned data
});

Taking "Node.js is Single Threaded" at face value, we'd take it to mean that the async block would execute its final callback after fifteen seconds, but knowing that "Node.js maintains its own internal thread pool", can we assume that Node would run each callback in its own thread, truly parallelise the functions, and call the final callback after 5 seconds?

Was it helpful?

Solution

Short answer: your code would take 15 seconds to execute. Why?

There's no way to run parallel code in javascript. The only way to run synchronous code without blocking the event loop would be to spawn a Worker. Since the async module doesn't care about workers, it would have to wait for your functions to be done before it can call the next one.

Consider the following:

sleep(5)
sleep(5);
sleep(5);

Obviously it would take 15 seconds. Now even if you make this "parallel" the way async does:

function parallel () {
  process.nextTick(function(){
    sleep(5);
  });
  process.nextTick(function(){
    sleep(5);
  });
}
parallel();
console.log("this is called immediately");

process.nextTick(function(){
  console.log("this is called 10 secs later");
});

The code is executed immediately, but as soon as the event loop yields to those queued up methods, they are gonna block other code from executing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top