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

async-await-retry

v2.1.0

Published

Simple module to retry a function with async/await syntax !

Downloads

27,978

Readme

GitHub release GitHub license CI pipeline Opened issues Opened PR DeepScan grade Code coverage Node version

Purpose

Minimalist, efficient and performance focused retry system. Basically it helps developer to retry a function with a specific interval, exponential factor etc.

No dependency.

Compatibility

/!\ This module use async/await syntax, this is why you must have node 7.6+.

Supported and tested : >= 7.6

| Version | Supported | Tested | | ------------- |:-------------:|:--------------:| | 20.x | yes | yes | | 18.x | yes | yes | | 16.x | yes | yes | | 14.x | yes | yes | | 12.x | no | yes | | 10.x | no | yes | | 9.x | no | yes | | 8.x | no | yes | | >= 7.6 | no | yes |

Installation

$ npm install async-await-retry --save

Usage

Basic usage

const retry = require('async-await-retry');

const func = async () => {return new Promise((resolve) => resolve('OK'))};

try {
    const res = await retry(func)
} catch (err) {
    console.log('The function execution failed !')
}

Sync function syntax

const retry = require('async-await-retry');

const func = () => {...};

try {
    const res = await retry(func)
    console.log(res) // output : OK
} catch (err) {
    console.log('The function execution failed !')
}

Anonymous function style

const retry = require('async-await-retry');

try {
    const res = await retry(async () => {
      return new Promise((resolve) => resolve('OK'))
    })
    
    console.log(res) // output : OK
} catch (err) {
    console.log('The function execution failed !')
}

Callback function style

const retry = require('async-await-retry');

try {
    const res = await retry((arg1, cb) => {
        ....
        cb(err, data); // send err as first argument
    }, ["arg1"], {isCb: true});
} catch (err) {
    console.log('The function execution failed !')
}

Options

## retry(function, [args], [config])

  • function : function to retry in case of error
  • args : your function's parameters in case you don't use callback style
  • config : an object containing all retry process options

options

| Option | description | Default value | | --------------- |:------------------------------------------------:|:----------------:| | retriesMax | Maximum number of retries | 3 | | interval | Delay in ms between two tentatives | 0 | | exponential | Will the interval increase exponentially ? | true | | maxBackoff | Maximum delay before to retry (with exponential) | 30s | | factor | The exponential factor to use | 2 | | jitter | Random jitter in ms to add to the interval | 0 | | isCb | Old callback function style ? | false | | onAttemptFail | User's callback to manage retry system | default fallback |

An example of custom options :

const retry = require('async-await-retry');

try {
    const res = await retry(async () => {
      return new Promise((resolve) => resolve('OK'))
    }, null, {retriesMax: 4, interval: 100, exponential: true, factor: 3, jitter: 100})
    
    console.log(res) // output : OK
} catch (err) {
    console.log('The function execution failed !')
}

onAttemptFail

This method can be used to manage, by yourself, the retry system. It's called when an error occurred and before to retry. This method can have three behaviors:

  • you can throw an error
  • if it returns truthy value then normal retry system continues
  • if it returns falsy value then the retry system stop
const retry = require('async-await-retry');

try {
    const res = await retry(MyfuncToRetry, null, {
        onAttemptFail: (data) => {
            // do some stuff here, like logging errors
        }
    });
} catch (err) {
    console.log('The function execution failed !')
}

The data argument is an object that can be described like this:

| Property | description | | --------------- |:------------------------------------------:| | error | The current error object | | currentRetry | The current retry value | | retriesMax | Maximum number of retries | | interval | Delay in ms between two tentatives | | exponential | Will the interval increase exponentially ? | | factor | The exponential factor to use | | jitter | Random jitter in ms to add to the interval | | maxBackoff | Maximum delay before to retry |

Test

$ npm test

Coverage report can be found in coverage/.