0.0.4 • Published 8 years ago

retrying-promise v0.0.4

Weekly downloads
118
License
-
Repository
github
Last release
8 years ago

retrying-promise

Create fault tolerant promises that retry upon failure according to a retry strategy.

The implementation of the retry strategies reuses code from the node-retry project.

Usage

Install retrying-promise:

npm install retrying-promise

Import retrying-promise:

const retryPromise = require('retrying-promise');

The constant retryPromise is a function that returns a promise. Use this function similar to how you call the new Promise() constructor, but pass it a function that takes three instead of two functions as arguments: resolve, retry and reject.

var promise = retryPromise(function (resolve, retry, reject) {

    resolve(result);  // the promise resolves normally

    retry(error);     // the promise failed and a retry may be attempted

    reject(error);    // the promise failed and no retry should be attempted

});

Call the resolve function when the promise resolves normally.

Call the retry function when the promise failed and a retry may be attempted.

Call the reject function when the promise failed and no retry should be attempted.

Test

Install Mocha.

sudo npm install -g mocha

Run tests:

npm test

API Reference

Author: Wouter Van den Broeck
Copyright: 2016

module.exports(options, executor) ⇒ Promise

Returns a promise that conditionally tries to resolve multiple times, as specified by the retry policy.

Kind: Exported function

ParamTypeDescription
optionsretryPolicyEither An object that specifies the retry policy.
executorretryExecutorA function that is called for each attempt to resolve the promise.

module.exports~createTimeout(attempt, opts) ⇒ number

Get a timeout value in milliseconds.

Kind: inner method of module.exports
Returns: number - The timeout value in milliseconds.

ParamTypeDescription
attemptnumberThe attempt count.
optsObjectThe options.

module.exports~retryPolicy : Object

An object that specifies the retry policy.

Kind: inner typedef of module.exports
Properties

NameTypeDefaultDescription
retriesnumber10The maximum amount of times to retry the operation.
factornumber2The exponential factor to use.
minTimeoutnumber1000The number of milliseconds before starting the first retry.
maxTimeoutnumberInfinityThe maximum number of milliseconds between two retries.
randomizebooleanfalseRandomizes the timeouts by multiplying with a factor between 1 to 2.

module.exports~retryExecutor : function

The function that is called for each attempt to resolve the promise.

Kind: inner typedef of module.exports

ParamTypeDescription
resolveFnfunctionTo be called when the promise resolves normally.
retryFnfunctionTo be called when the promise failed and a retry may be attempted.
rejectFnfunctionTo be called when the promise failed and no retry should be attempted.

© 2016, Wouter Van den Broeck