It would be really great if bluebird would have retry functionality built in. I have built this a few times and I have heard the same from colleagues.
Proposing a new top-level API Promise.retry. Would you be willing to have this in bluebird? If so, I am willing to contribute this.
Promise.retry(() => {
// Will internally call Promise.resolve(resultOfThisFunction)
// and potentially Promise.timeout() when a duration is specified
}, {
// either retries or duration is required, a combination is possible
retries: <number>,
duration: <number, millis>,
// optional, to support exponential backoff and randomized intervals
getTryDelay(attempt: number): number {
// exponential backoff
return Math.pow(attempt, 2);
}
})
Is something like exponential backoff and randomized interval necessary?
@kontrafiktion: This could be an extension point. Maybe via an option which would allow the user to configure a delay until a next retry. I updated the API proposal to include this via getTryDelay
Promises are composable, unless you can prove a performance benefit - this can be in a library:
let retry = async (fn, retries = 3, backoff = () => 100) => async (...args) => {
for(let i = 0; i < retries; i++) {
try {
await fn(...args);
} catch (e) {
if(i === retries - 1) throw e;
await backoff();
}
}
};
You might find p-retry useful.
Most helpful comment
Promises are composable, unless you can prove a performance benefit - this can be in a library: