2016-11-13 69 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); 
}); 
相關問題