Giter Site home page Giter Site logo

pedrohugorm / node-promise-retry Goto Github PK

View Code? Open in Web Editor NEW

This project forked from indigounited/node-promise-retry

0.0 1.0 0.0 82 KB

Retries a function that returns a promise, leveraging the power of the retry module.

License: MIT License

JavaScript 100.00%

node-promise-retry's Introduction

node-promise-retry

NPM version Downloads Build Status Dependency status Dev Dependency status Greenkeeper badge

Retries a function that returns a promise, leveraging the power of the retry module to the promises world.

There's already some modules that are able to retry functions that return promises but they were rather difficult to use or do not offer an easy way to do conditional retries.

Installation

$ npm install promise-retry

Usage

promiseRetry(fn, [options])

Calls fn until the returned promise ends up fulfilled or rejected with an error different than a retry error.
The options argument is an object which maps to the retry module options:

  • retries: The maximum amount of times to retry the operation. Default is 10.
  • factor: The exponential factor to use. Default is 2.
  • minTimeout: The number of milliseconds before starting the first retry. Default is 1000.
  • maxTimeout: The maximum number of milliseconds between two retries. Default is Infinity.
  • randomize: Randomizes the timeouts by multiplying with a factor between 1 to 2. Default is false.

The fn function will receive a retry function as its first argument that should be called with an error whenever you want to retry fn. The retry function will always throw an error.
If there are retries left, it will throw a special retry error that will be handled internally to call fn again. If there are no retries left, it will throw the actual error passed to it.

If you prefer, you can pass the options first using the alternative function signature promiseRetry([options], fn).

Example

var promiseRetry = require('promise-retry');

// Simple example
promiseRetry(function (retry, number) {
    console.log('attempt number', number);

    return doSomething()
    .catch(retry);
})
.then(function (value) {
    // ..
}, function (err) {
    // ..
});

// Conditional example
promiseRetry(function (retry, number) {
    console.log('attempt number', number);

    return doSomething()
    .catch(function (err) {
        if (err.code === 'ETIMEDOUT') {
            retry(err);
        }

        throw err;
    });
})
.then(function (value) {
    // ..
}, function (err) {
    // ..
});

Tests

$ npm test

License

Released under the MIT License.

node-promise-retry's People

Contributors

achingbrain avatar greenkeeperio-bot avatar ryandanielspmc avatar satazor avatar tgabi333 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.