简体   繁体   中英

Node.JS async.parallel doesn't wait until all the tasks have completed

I am using aync.parallel to run two functions in parallel. The functions request RSS feeds. Then the RSS feeds are parsed and added to my web page.

But for some reason async.parallel runs the callback method without waiting until the two functions have completed

The documentation says:

Once the tasks have completed, the results are passed to the final callback as an array.

My code.

require('async').parallel([ function(callback) {
        fetchRss(res, bbcOpts); // Needs time to request and parse
        callback();
    }, function(callback) {
        // Very fast.
        callback();
    } ], function done(err, results) {
        if (err) {
            throw err;
        }
        res.end("Done!");
    });

In fact I only have "Done!" on my web page. Why?

Why do I need to call res.end() ?

The Node.JS documentation says:

The method, response.end(), MUST be called on each response.

If I don't call it, my web page will be being "downloaded" (I mean a progress bar in the address line of my browser).

I suppose your fetchRss function is asynchronous: is is performed later and the callback is immediately called. You should send the callback to fetchRss :

function fetchRss(res, bbcOpts, callback) {
    doSomething();
    callback();
}

require('async').parallel([ function(callback) {
        fetchRss(res, bbcOpts, callback); // Needs time to request and parse
    }, function(callback) {
        // Very fast.
        callback();
    } ], function done(err, results) {
        if (err) {
            throw err;
        }
        res.end("Done!");
    });

On a side note, you should call res.end() in order for Node to know that the message is complete and that everything (headers + body) has been written. Otherwise, the socket will stay open and will wait forever for another message (which is why the browser shows a progress bar: it doesn't know that the request has ended).

您可以使用async.reflectasync.reflectAll来确保所有函数都已完成http://caolan.github.io/async/docs.html#reflect

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM