Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何确保在循环完成后执行语句?_Javascript_Node.js_Asynchronous_Express - Fatal编程技术网

Javascript 如何确保在循环完成后执行语句?

Javascript 如何确保在循环完成后执行语句?,javascript,node.js,asynchronous,express,Javascript,Node.js,Asynchronous,Express,下面是routes/index.js中我的代码的快照 exports.index = function(req, res){ var results=new Array(); for(var i=0; i<1000;i++){ //do database query or time intensive task here based on i // add each result to the results array }

下面是routes/index.js中我的代码的快照

exports.index = function(req, res){
    var results=new Array();
    for(var i=0; i<1000;i++){
        //do database query or time intensive task here based on i 
        // add each result to the results array
    }
    res.render('index', { title: 'Home !' , results:results });
};
使用:

如果循环中的一个任务是异步的,则需要向异步任务传递一个调用
callback()
的回调。如果在
forEach
中没有要使用的数组,只需用整数1-1000填充一个数组即可

编辑:给定最新代码,只需将
async
callback()
放在
responses.push(reply.name)
exports.index=function(req,res)之后{
var事件=要求(“事件”);
var e=new events.EventEmitter();
e、 预期为1000;
e、 完成=0;
e、 结果=[];
e、 在(“finishedQuery”上,(函数(err,r){
此参数为+=1;
this.results.push(r&&r.name);
if(this.finished==this.expected){
res.render('index',{title:'Home!',results:this.results});
};
}).约束(e));
对于(变量i=0;i

当然,上面的代码不会处理[1个或多个]错误。您需要添加只在第一个错误时响应(1)或在没有错误发生时响应(2)的逻辑

循环体中到底是什么?它支持回调吗?嗨,zerkms-我刚刚更新了问题,添加了关于该部分的详细信息。是的,它支持回调。我不认为你的第二个版本比OP的原始版本更好。第一个很好,但确保在将结果添加到结果数组后调用
callback
,而不仅仅是在循环体的末尾。是的,请参阅关于将i增量传递到callback的已编辑答案。请参阅我关于在任何异步处理后递增i并通过回调执行条件检查的说明。在第二个示例中,某些项目将运行多次。非异步for循环不起作用。
client.hgetall("game:" +i, function(err, reply) {

           results.push(reply.name);
        });
exports.index = function(req, res){
    var results=new Array();
    async.forEach(someArray, function(item, callback){
        // call this callback when any asynchronous processing is done and
        // this iteration of the loop can be considered complete
        callback();
    // function to run after loop has completed
    }, function(err) {
        if ( !err) res.render('index', { title: 'Home !' , results:results });
    });
};
exports.index = function(req, res) {
  var events = require("events");
  var e = new events.EventEmitter();
  e.expected = 1000;
  e.finished = 0;
  e.results = [];

  e.on("finishedQuery", (function(err, r) {
    this.finished += 1;
    this.results.push(r && r.name);
    if (this.finished === this.expected) {
       res.render('index', { title: 'Home !' , results:this.results });
    };
  }).bind(e));

  for (var i = 0; i < e.expected; i++) {
    client.hgetall("game:" + i, function(err, reply) {
      e.emit("finishedQuery", err, reply);
    });
  };
};