Promise retry system for Q
Qretry creates a Promise from the call function with a retry strategy.
bower install qretry
Also available on NPM.
Qretry takes an action function and returns a Promise of result where result is the first successful value resulting from the action call or a failure if the retry reached his limit (this failure Promise is the last failed Promise from call).
Qretry(action: (=> R or Promise[R]), options: Object) => Promise[R]
options is an object with optional parameters:
maxRetry(Number) optional: set the maximum retry (default is 5)
interval(Number) optional: set the initial interval in milliseconds between the first and the second call. (default is 500)
intervalMultiplicator(Number >= 1) optional: set the multiplicator which increase the interval through tries. (default is 1.5)
var promise = Qretryreturn eventuallyResult;;
Qretryreturn QajaxgetJSON"/item.json";maxRetry: 3thenconsole.logitem;;
var startTime = ;Qretryconsole.log"action at "+-startTime/1000+"s";// this retry system also work with exceptions (Q unifies exceptions as rejected Promise)if Mathrandom<0.8 throw "failure";return "<success!>";maxRetry: 8 interval: 100 intervalMultiplicator: 2thenconsole.logitem;console.errorerr;;
will eventually log:
action at 0saction at 0.101saction at 0.303saction at 0.704saction at 1.506saction at 3.108saction at 6.31s<success!>
if action fail 6 times and succeed at the 7th time.