Home

Awesome

Qretry Build Status

Qretry creates a Promise from the call function with a retry strategy.

Checkout the Annotated Source Code

Installation

bower install qretry

Also available on NPM.

Usage

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:

Simple example

var promise = Qretry(function () {
  return eventuallyResult();
});

Example with Qajax

Qretry(function () {
    return Qajax.getJSON("/item.json");
}, { maxRetry: 3 })
.then(function (item) {
  console.log(item);
});

Random example

var startTime = new Date();
Qretry(function () {
  console.log("action at "+((new Date()-startTime)/1000)+"s");
  // this retry system also work with exceptions (Q unifies exceptions as rejected Promise)
  if (Math.random()<0.8) throw "failure";
  return "<success!>";
}, { maxRetry: 8, interval: 100, intervalMultiplicator: 2 })
.then(function (item) {
  console.log(item);
}, function (err) {
  console.error(err);
});

will eventually log:

action at 0s
action at 0.101s
action at 0.303s
action at 0.704s
action at 1.506s
action at 3.108s
action at 6.31s
<success!>

if action fail 6 times and succeed at the 7th time.