Understanding Node.JS async.parallel

73,382

If you want to be absolutely certain in the order in which the results are printed, you should pass your data (abc\n and xyz\n) through the callbacks (first parameter is the error) and handle/write them in the final async.parallel callback's results argument.

async.parallel({
    one: function(callback) {
        callback(null, 'abc\n');
    },
    two: function(callback) {
        callback(null, 'xyz\n');
    }
}, function(err, results) {
    // results now equals to: results.one: 'abc\n', results.two: 'xyz\n'
});
Share:
73,382

Related videos on Youtube

Maksim Dmitriev
Author by

Maksim Dmitriev

My name is Maksim Dmitriev. I've been developing Android apps since September 2011. In 2014 I earned 3 Coursera certificates. I write code for many devices, screens, and versions of Android, including upcoming versions of the OS. My LinkedIn profile

Updated on April 15, 2020

Comments

  • Maksim Dmitriev
    Maksim Dmitriev about 4 years

    I need to request data from two web servers. The tasks are independent; therefore, I am using aync.parallel. Now I am only writing 'abc', 'xyz', and 'Done' to the body of my web page.

    Since tasks are performed at the same time, can I run into a strange output? E.g.,

    xab
    cyz
    

    The code.

    var async = require('async');
    
    function onRequest(req, res) {
        res.writeHead(200, {
            "Content-Type" : "text/plain"
        });
    
        async.parallel([ function(callback) {
            res.write('a');
            res.write('b');
            res.write('c\n');
            callback();
        }, function(callback) {
            res.write('x');
            res.write('y');
            res.write('z\n');
            callback();
        } ], function done(err, results) {
            if (err) {
                throw err;
            }
            res.end("\nDone!");
        });
    
    }
    
    var server = require('http').createServer(onRequest);
    server.listen(9000);
    
    • Bulkan
      Bulkan over 10 years
      The order of what is written to res is dependant on which of the async.parallel task finishes first but as the tasks are independent than the order shouldn't matter.
    • Maksim Dmitriev
      Maksim Dmitriev over 10 years
      @Bulkan, thank you. But parallel seems not to work properly. Please read a new question.
  • Maksim Dmitriev
    Maksim Dmitriev over 10 years
    I can pass the arguments, but I should perform long operations within the parallel functions
  • Michael Tang
    Michael Tang over 10 years
    I was assuming you were doing some long operation to get the two strings (maybe from a database), suggesting separating the code for retrieving data (parallel) and processing (res.write) it (sequential).
  • Adam F
    Adam F almost 9 years
    That is Genius, Thanks
  • Green
    Green over 7 years
    Strange conversation you have here, guys. Did Michael Tang gave correct answer? Not clear
  • Michael Tang
    Michael Tang over 7 years
    @Green I think some of the comments were lost... basically what I meant was that the writing of the data to the console is something one wants to be synchronized, and thus should be executed in the parallel's callback. The functions in the first argument to parallel should be in charge of getting any data (that can be done asynchronously in any order) and collecting it for parallel's callback. In this case we're calling parallel with trivial functions, but one can imagine reading from disk or the network in its place.