0.1.5 • Published 10 years ago

q-retry v0.1.5

Weekly downloads
437
License
MIT
Repository
github
Last release
10 years ago

Q-retry plugin v0.1 for Q

TypeScript definitions available (DefinitelyTyped).

Install

npm install q-retry

Note

Q-retry set Q as peer dependency, it will add retry method directly to the same Q you were working with, but will not add retry to the nested Q module of other modules and its instance.

So make sure the promise you want to use retry on is created or wrapped by non-nested Q module. (Currently simply Q(promise) will return the original Q promise directly, I have created a pull request https://github.com/kriskowal/q/pull/566 and hope they will have it merged.)

Use

var Q = require('q-retry');

Q
    .retry(function () {
        if (Math.random() < 0.8) {
            throw new Error('err 1');
        }

        return Q.delay(Math.floor(Math.random() * 1000))
            .then(function () {
                if (Math.random() < 0.8) {
                    throw new Error('err 2');
                }
                return 'expected result.';
            });
    }, function (reason, retries) {
        console.log('failed with ' + reason + ', ' + retries + ' retries left.');
    }, 100)
    .then(function (str) {
        console.log(str);
    })
    .fail(function (reason) {
        console.log(reason);
    });

API

/**
 * retry a promise process and return a promise with the fulfilled value.
 * @param process a function like onFulfilled for then method.
 * @param onFail a function takes the reason and the number of left retries as parameters.
 * @param options a number represents for retries limit or an object for more options.
 */
Q.retry(process, [onFail], [options]);
promise.retry(process, [onFail], [options]);

options:

limit a number represents for retries limit.
interval interval (milliseconds) between each retries.
maxInterval max interval when interval multiplier applied.
intervalMultiplier the multiplier applies to interval after every retry.

0.1.5

10 years ago

0.1.4

10 years ago

0.1.3

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago