Gary Gary - 1 month ago 9
Node.js Question

nodejs Async's whilst

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

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