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
async. whilst will call the function each time the test passes. async. until will call the function each time the test fails.
async. eachSeries() applies an asynchronous function to each item in an array in series. For example, say you have a list of users, each of which needs to post its profile data to remote server log. Order matters in this case because the users in your array are sorted.
Example of async. parallel([ function(callback) { setTimeout(function() { console. log('Task One'); callback(null, 1); }, 200); }, function(callback) { setTimeout(function() { console. log('Task Two'); callback(null, 2); }, 100); } ], // optional callback function(err, results) { console.
waterfall: This waterfall method runs all the functions(i.e. tasks) one by one and passes the result of the first function to the second, second function's result to the third, and so on. When one function passes the error to its own callback, then the next functions are not executed.
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 If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With