【发布时间】:2013-04-23 06:04:45
【问题描述】:
在异步并行请求之后部分呈现视图的正确方法是什么?
目前我正在做以下事情
// an example using an object instead of an array
async.parallel({
one: function(callback){
setTimeout(function(){
callback(null, 1);
// can I partially merge the results and render here?
}, 200);
},
two: function(callback){
setTimeout(function(){
callback(null, 2);
// can I partially merge the results and render here?
}, 100);
}
},
function(err, results) {
// results is now equals to: {one: 1, two: 2}
// merge the results and render a view
res.render('mypage.ejs', { title: 'Results'});
});
它基本上工作正常,但是,如果我有 function1, function2, ..., functionN,则只有在最慢的功能完成时才会呈现视图。
我想找到正确的方法,以便能够在第一个函数返回时立即渲染视图,以最大限度地减少用户延迟,并在函数结果可用时立即添加。
【问题讨论】:
-
如果功能以随机顺序完成,您希望它如何工作?我怀疑你可以像这样发送部分数据(以随机顺序)。您是否考虑过从客户端执行多个部分请求?
标签: node.js asynchronous express