2016-11-13 126 views
0

虽然我使用的是async.until()函数,但我发现测试没有被重复调用,尽管它返回false。因此函数没有被调用来执行下一个任务。在async.until(测试,函数,回调)测试函数没有被重复调用

var resultReceived = false; 
async.until(function(){ 
    console.log("Checking result : "+resultReceived); 
      return resultReceived; 
}, function(callback){ 
    try{ 
     //do something 
     resultReceived = true; 
    }catch(err){ 
     resultReceived = false; 
    } 
}, function(result){ 
      console.log("===================="); 
      console.log(result); 
      callback(result); 
}); 
+0

是 - 因为异步意味着它不会等待以前的执行在启动之前完成,这是下一步 - 看到这个答案 http://stackoverflow.com/questions/748175/asynchronous-vs-synchronous-execution-what-does-it-really-mean – mike510a

+0

是的,我明白。但是如果你检查这个文档[https://caolan.github.io/async/docs.html#until]它说它重复调用main函数,直到测试返回true。这就是为什么我想知道.. – Angshuman

回答

1

您只能看到测试函数被调用一次的原因是因为您从未超过第一次迭代。为了从一个迭代移动到下一个迭代,需要在传递到async.until的第二个函数中调用callback

这里有一个稍微修改示例,将显示测试功能与每个迭代被调用:

var iteration = 0; 
async.until(function(){ 
    console.log("Checking iteration : " + iteration); 
    return iteration > 5; 
}, function(callback){ 
    console.log(++iteration); 
    setTimeout(callback, 1000); 
}, function(result){ 
    console.log("===================="); 
    console.log(result); 
});