npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

retriable-promise

v1.0.0

Published

Add retry logic to a promise-returning function

Downloads

4

Readme

retriable-promise

Add retry logic to a Promise-returning function.

npm Version Build Status Test Coverage Dependency Status

Features

  • Composable. By wrapping your existing functions, retry logic is easily integrated into your applications.
  • Simple. The API surface area is minimal but powerful.
  • Tiny. 0 external dependencies. < 30 LOC.
  • Flexible. Works with Bluebird, jQuery, RSVP, and pretty much every other Promise library out there.

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) → Function
const retriable = require('retriable-promise');
const api = require('./my-api-module');

// Create a new function that wraps your existing function with retry logic.
const fetchTweetsWithRetries = retriable(api.fetchTweets, {
  // Retry 3 times, delayed 0/500/1000 ms respectively
  retries: [0, 500, 1000],

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

fetchTweetsWithRetries(/* any args to original function */)
  .then(/* on success */)
  .catch(/* on failure */);

Why Another Retry Library?

An npm search brings up half a dozen retry libraries, if not more. promise-retry has hundreds of thousands of downloads. Why not use one of those? Well, for me, I wanted something more lightweight. In my experience, the retry logic I typically add is finite and well understood. Most often, it's retry up to three times, with some form of linear or exponential backoff. This doesn't require any sort of algorithms to work out how and when the retries should be invoked. With such a small number, it's trivial to figure out how long to wait and list out the delays manually. This concept is simple, but deceivingly capable, as you have the flexibility to devise any discrete series of timeouts. This is what primarily drove the API I came up with. Secondarily, I needed a jQuery-compatible solution that could easily decorate existing functions and methods.

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