Helper for making an async function stubborn (retry the operation on failure)
Create a "stubborn" version of an asynchronous function so that the operation is retried a configurable number of times in case the original function returns an error.
var stubbornizeAsync = require'stubbornizeasync';// This function will fail ~10% of the timesetTimeoutif Mathrandom > 9cb"whoops";elsecb;10;// This function will fail ~1% of the time:var stubbornFunction = stubbornizeAsyncmyFlakyFunction numRetries: 1;// This function will fail ~0.01% of the time:var evenMoreStubbornFunction = stubbornizeAsyncmyFlakyFunction numRetries: 3;evenMoreStubbornFunctionif errthrow err;else// Yay!;
By default the stubbornized function retries the original function 3
times (so it's run a maximum of 4 times), and it waits 250
milliseconds between the attempts. These parameters can be adjusted by
providing an options object as the second parameter to
var veryStubbornAndVeryPatientFunction = stubbornizeAsyncmyFlakyFunction numRetries: 10 delayMsec: 10000;
Make sure you have node.js and npm installed, then run:
npm install stubbornizeasync
3-clause BSD license -- see the
LICENSE file for details.