admin管理员组文章数量:1291031
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 pleted
The documentation says:
Once the tasks have pleted, 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 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 pleted
The documentation says:
Once the tasks have pleted, 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).
Share Improve this question edited Nov 25, 2013 at 10:15 Paul Mougel 17k6 gold badges59 silver badges65 bronze badges asked Nov 25, 2013 at 7:22 Maksim DmitrievMaksim Dmitriev 6,20913 gold badges80 silver badges141 bronze badges2 Answers
Reset to default 10I 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 plete 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).
You can use async.reflect or async.reflectAll to do make sure all functions are pleted http://caolan.github.io/async/docs.html#reflect
本文标签: javascriptNodeJS asyncparallel doesn39t wait until all the tasks have completedStack Overflow
版权声明:本文标题:javascript - Node.JS async.parallel doesn't wait until all the tasks have completed - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741509383a2382516.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论