Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 790
Gary Avatar asked Mar 13 '13 18:03

Gary


People also ask

What is async whilst?

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

How does async eachSeries work?

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.

How do I use async parallel in node JS?

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.

What is async waterfall?

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.


2 Answers

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
    }
);
like image 90
Chad Avatar answered Sep 24 '22 16:09

Chad


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
like image 35
Pablo Carbajal Avatar answered Sep 20 '22 16:09

Pablo Carbajal