guilin 桂林
guilin 桂林

Reputation: 17422

Nodejs: parallel request, serial response

I have done Nodejs: How to write high performance async loop for stormjs, you can check stormjs serial loop demo

but there is still have problem that parallel loop, e.g. we have a function requestContext(index, callback(err, context)) which remote get context 'http://host/post/{index}', and we need get the context of [0-99] and push context into an array in the order, [context0...context99]

but obviously this output cant work stormjs parallel loop

I still want to know how noders do this task, but you must make these requests parallel, not 1 by 1, it should be parallel request and serial push.

Upvotes: 0

Views: 948

Answers (1)

Raynos
Raynos

Reputation: 169531

var counter = 0;
// create an array with numbers 0 to 99.

_.range(0,100).forEach(function(key, value, arr) {
    // for each of them request context
    requestContext(key, function(err, context) {
        // add the context to the array under the correct key
        if (!err) {
            arr[key] = context;
        }
        // increment counter, if all have finished then fire finished.
        if (++counter === 100) {
            finished(arr);
        }
    });
});

function finished(results) {
    // do stuff
}

No storm required. If you want an execution / flow control library I would recommend Futures because it doesn't compile your code and "hides the magic".

Previously you recursed through each one and executed them in a serial order pushing them into the array in order.

This time you execute them all in parallel and tell each one to assign the right ordered key in the array to their own value.

_.range Creates an array containing values 0 to 99.

Upvotes: 1

Related Questions