0

While I was using async.until() function I observed that the test is not getting invoked repetitively though it is returning false. As a result function is not getting invoked to perform the next task.

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);
});
Cœur
  • 37,241
  • 25
  • 195
  • 267
Angshuman
  • 225
  • 3
  • 17
  • Yes - this is because asynchronous means that it doesn't wait for the previous execution to complete before starting the next -- see this answer http://stackoverflow.com/questions/748175/asynchronous-vs-synchronous-execution-what-does-it-really-mean – mike510a Nov 13 '16 at 14:12
  • Yes I understand. but If you check this documentation [https://caolan.github.io/async/docs.html#until] it says it repeatedly call main function until test returns true. That's why I was wondering.. – Angshuman Nov 13 '16 at 14:26

1 Answers1

1

The reason you only see the test function invoked once is because you never advance past the first iteration. In order to move from one iteration to the next, you need to invoke callback in the second function that you pass to async.until.

Here is a slightly modified example that will show the test function being invoked with each iteration:

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);
});
andyk
  • 1,628
  • 15
  • 12