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);
2
  • 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. Commented Nov 25, 2013 at 6:45
  • @Bulkan, thank you. But parallel seems not to work properly. Please read a new question. Commented Nov 25, 2013 at 7:23

1 Answer 1

59

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'
});
Sign up to request clarification or add additional context in comments.

4 Comments

I can pass the arguments, but I should perform long operations within the parallel functions
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).
Strange conversation you have here, guys. Did Michael Tang gave correct answer? Not clear
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.