nodejs Async's whilst

Gary picture Gary · Mar 13, 2013 · Viewed 19.8k times · Source

Greeting all,

I want to call a function repeatedly, but wanted each call to run only when the previous call is completed. Does the Async's whilst fit what I need? Or do the calls happen in parallel?

Thanks!

Gary

Answer

Chad picture Chad · Mar 13, 2013

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
    }
);