Socket
Socket
Sign inDemoInstall

@housinganywhere/async-retry

Package Overview
Dependencies
0
Maintainers
4
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @housinganywhere/async-retry

Async retry with hooks


Version published
Maintainers
4
Install size
15.1 kB
Created

Readme

Source

@housinganywhere/async-retry

npm version bundle size CircleCI license

Problem

We needed a utility that helps us to retry an async function on it's Failure/Rejection and to be able to control the number of retries and the time interval between each retry also to block the retries in case of some logic provided.

Installation
yarn add @housinganywhere/async-retry

Or if you prefer npm!

npm i @housinganywhere/async-retry
Usage
import asyncRetry from '@housinganywhere/async-retry';

asyncRetry expects two parameters

  • The first parameter is a function that returns a Promise.
  • The second parameter is an options object (all of them are optional).
OptionsTypeOptional?DefaultDescription
retriesnumber:heavy_check_mark:5retriesnumber of retries
intervalnumber:heavy_check_mark:5000msbase interval between retries
dontRetryfunction:heavy_check_mark:() => falseA function that returns a boolean value. This boolean value is to check if we want the retry to continue or not
onCompletefunction:heavy_check_mark:nullA hook function that's called on the completion of the retry also provides (err, count) in params
onFailurefunction:heavy_check_mark:nullA hook function that's called on the failure of the retry also provides (err) in params
onRetryfunction:heavy_check_mark:nullA hook function that's called on every retry also provides (err, count) in params
Example
import asyncRetry from '@housinganywhere/async-retry';

const tryFetch = () => fetch('http://www.mocky.io/v2/5c59705d320000f31eba3880')
  .then(res => {
    if (res.status !== 200) {
      return Promise.reject(new Error(`Rejected because of statusCode is ${res.status}`));
    }

    return res.json();
  }).catch(e => Promise.reject(new Error(e)));

asyncRetry(tryFetch, { 
  retries: 3,
  onRetry: (err, count) => console.log(`#### Retry #${count} with ${err}.`),
  onComplete: count => console.log(`#### Completed after ${count} retries.`),
  onFailure: err => console.log(`#### Failed because ${err}.`),
})
  .then(response => {
    console.log({response})
  })
  .catch(console.error);
Inspiration
Thanks
License

MIT License © housinganywhere

Keywords

FAQs

Last updated on 11 Sep 2019

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc