Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

async-await-retry

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-await-retry

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

  • 2.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source
Async / Await exponential retry


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

VersionSupportedTested
20.xyesyes
18.xyesyes
16.xyesyes
14.xyesyes
12.xnoyes
10.xnoyes
9.xnoyes
8.xnoyes
>= 7.6noyes

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

Optiondescription Default value
retriesMaxMaximum number of retries3
intervalDelay in ms between two tentatives0
exponentialWill the interval increase exponentially ?true
maxBackoffMaximum delay before to retry (with exponential)30s
factorThe exponential factor to use2
jitterRandom jitter in ms to add to the interval0
isCbOld callback function style ?false
onAttemptFailUser's callback to manage retry systemdefault 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:

Propertydescription
errorThe current error object
currentRetryThe current retry value
retriesMaxMaximum number of retries
intervalDelay in ms between two tentatives
exponentialWill the interval increase exponentially ?
factorThe exponential factor to use
jitterRandom jitter in ms to add to the interval
maxBackoffMaximum delay before to retry

Test

$ npm test

Coverage report can be found in coverage/.

Keywords

FAQs

Package last updated on 21 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc