0

I want to perform a set of async tasks repeatedly in a loop. I tried using async library's waterfall approach to do something like

while(somethingIsTrue) {
     async.waterfall([
         function(callback) { ... },
         function(callback) { ... },

     ], function(err) {
         ...
        }
     );
}

The problem I encountered was that the the first async function never got scheduled or rather executed and the loop kept on running, obviously because the loop didnt wait for the async functions to finish. I'm not so well verse with asynchronous programming and I would need help to come up with some pattern to solve this problem.

1 Answer 1

1

Try a semi-recursive solution such as

function again_and_again(callback) {
    if (!somethingIsTrue) return;
    async.waterfall([
         function(callback) { ... },
         function(callback) { ... },
    ], function(err, result) {
        if (err) return callback(err);
        again_and_again(callback);
    });
}

The idea is to wait for the waterfall to finish, and arrive at the callback, and repeat the "loop" there by calling again_and_again again.

Sign up to request clarification or add additional context in comments.

1 Comment

Alternatively, async#whilst might also be useful.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.