Retries generators that throw an error.
In your project folder, type:
npm install co-retry
Suppose you have a generator function (or any yieldable object constructors supported by co):
var job = function *() {
console.log('Doing something...');
throw new Error('Failure!!!');
};
Just wrap that function with co-retry before yielding it.
var retry = require('co-retry');
yield retry(job);
co-retry will re-run your generator if it throws an error.
yield retry(job);
If you need to call fn
with a context and some arguments, simply use the ES5 bind
feature.
yield retry(job.bind(ctx, arg1, arg2));
The following options are available:
The number of times to retry fn
before giving up and rethrowing the last error caught. Default: 6.
// In case of error, job will be called 1 + 10 times.
yield retry(job, { retries: 10 });
The number of milliseconds to wait between attempts. Default: 1000.
The factor by which the interval should be multiplied per attempt. If set to 2 with an interval of 5000, the first retry will execute after 5 seconds, the second after 10, the third after 20, and so on. Default: 2.
This allows an exponential back-off scheme. For a smaller gap between retries, floats like 1.2 can be used to grow the interval at a slower rate.
API and documentation heavily inspired from node-attempt created by Tom Frost. Thanks to him.
co-retry is distributed under the MIT license.