nodejs Async's whilst

Whilst will do what you need, it runs each function in series. Before each run it will do the "test" function to make sure it should run again.

Their example:

var count = 0;

async.whilst(
    function () { return count < 5; },
    function (callback) {
        count++;
        setTimeout(callback, 1000);
    },
    function (err) {
        // 5 seconds have passed
    }
);

As Chad noted, Async's whilst will do the job.

You may want to consider Async's until (inverse of whilst). Both do the same job however the key difference is:

  • async.whilst will call the function each time the test passes
  • async.until will call the function each time the test fails