Skip to content

Latest commit

 

History

History
75 lines (54 loc) · 2.42 KB

README.md

File metadata and controls

75 lines (54 loc) · 2.42 KB

retriable-promise

Add retry logic to a Promise-returning function.

npm Version Build Status Test Coverage Dependency Status

Installation

Install using npm:

$ npm install retriable-promise

Usage

retriable-promise exports a function that accepts a Promise-returning function and an options object, returning a new function that decorates the given one with retry logic:

retriable(func, options)
const retriable = require('retriable-promise');
const api = require('./my-api-module');

const fetchStuff = retriable(api.fetchStuff, {
  // Retry 3 times, delayed 0/500/1000 ms respectively
  retries: [0, 500, 1000],

  // Only when the status code is 429
  when(err) {
    return err.statusCode === 429;
  }
});

Available Options

options.retries (Array, required)

An array of integers, representing the delay (in ms) to wait before invoking each retry attempt. For example, passing { wait: [1000, 1000, 1000] } would configure the returning function to retry up to 3 times, waiting 1 second before each subsequent attempt.

options.when (Function)

By default, retries will be invoked for all Promise rejections. In order to refine which failures are retriable, specify a when function that receives the Promise rejection value as an argument and returns true or false depending on whether a retry should be made.

options.Promise (Function)

The particular Promise implementation can be overridden by specifying options.Promise, which defaults to the native ES2015 Promise. Note that if this option is specified, the given function should conform to the native Promise constructor API, i.e., it is expected to take a callback function that itself receives resolve and reject callbacks and returns an appropriate promise instance.

License

MIT