26

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);
supertrue
  • 2,049
  • 5
  • 30
  • 49
Maksim Dmitriev
  • 5,985
  • 12
  • 73
  • 138
  • 1
    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. – Bulkan Nov 25 '13 at 06:45
  • @Bulkan, thank you. But `parallel` seems not to work properly. Please read [a new question](http://stackoverflow.com/questions/20186787/node-js-async-parallel-doesnt-wait-until-all-the-tasks-have-completed). – Maksim Dmitriev Nov 25 '13 at 07:23

1 Answers1

60

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'
});
Meet Zaveri
  • 2,921
  • 1
  • 22
  • 33
Michael Tang
  • 4,686
  • 5
  • 25
  • 24
  • I can pass the arguments, but I should perform long operations within the parallel functions – Maksim Dmitriev Nov 25 '13 at 10:40
  • 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). – Michael Tang Nov 25 '13 at 20:36
  • That is Genius, Thanks – Adam Fowler Jul 15 '15 at 20:59
  • Strange conversation you have here, guys. Did Michael Tang gave correct answer? Not clear – Green Jan 21 '17 at 04:45
  • @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. – Michael Tang Jan 21 '17 at 08:04