Sunday, 12 March 2017

node async module: combine parallel with retry

Here's a simple example of the use of async.parallel:

var fakeTimer = 0;
async.parallel({
    one: function(callback) {
        if (fakeTimer < 2) {
            callback(new Error('too soon!'), null);
            fakeTimer++;
        } else {
            callback(null, 'I am one');
        }
    },
    two: function(callback) {
        callback(null, 'I am two');
    }
}, function(err, results) {
    if (err) {
        console.log('failed!');
    } else {
        console.log(results);
    }
});

When this runs, of course it always ends in failure. What I'd like to do is keep retrying until fakeTimer has become large enough that the one function succeeds.

So either the whole async.parallel could be retried e.g. 5 times, or just the one function. I know that there is the async.retry feature, but I just can't get my head around how to combine that with async.parallel to achieve what I want.

I think ideally the whole async.parallel should be retried, so that it works if the error happens in any of the parallel branches, but it would be great to see an example of an overall retry and a per-branch retry.



via drmrbrewer

No comments:

Post a Comment