New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@dyaa/async-retry

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dyaa/async-retry

Async retry with hooks

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-83.33%
Maintainers
1
Weekly downloads
 
Created
Source

@dyaa/async-retry

wercker status

Table of Contents

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 array with http status codes provided!

Installation
yarn add @dyaa/async-retry

Or if you prefer npm!

npm i @dyaa/async-retry
Usage
import asyncRetry from '@dyaa/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
retriesnumberoptional5retriesnumber of retries
intervalnumberoptional5000msbase interval between retries
dontRetryfunctionoptional() => falseA function that returns a boolean value. This boolean value is to check if we want the retry to continue or not
onCompletefunctionoptionalnull.
onFailurefunctionoptionalnull.
onRetryfunctionoptionalnull.
Example
import asyncRetry from '@dyaa/async-retry';
// import initRealtimeService action...
// import ResolvedType

/*
As we said first parameter is a function that returns a promise.
so we gonna try with function that dispatches an action for this example
*/
const fn = () => dispatch(initRealtimeService());

asyncRetry<ResolvedType, NetworkError>(fn, {
	retries: 2, // default is 5
	interval: 2000, // default is 5000
	dontRetry: (e: NetworkError) => [404, 503].includes(e.status),
}).then().catch();

The example above says that we gonna retry two times the function in case of it's Failure/Rejection with base interval of 2000 but if we got status codes 404 or 503 we won't retry at all.

Backoff formula
/**
 * Backoff formula for more info checkout
 * https://en.wikipedia.org/wiki/Exponential_backoff
 * E -> factor
 * c -> number of trys
 * I -> Interval
 * 
 * E(c) = (2^c - 1) / 2
 * E(c) * I
 * @param {number} times - The number of which retry.
 * @param {number} interval - Time interval of last retry in ms.
 * @returns {number} - Number of next retry interval in ms.
 */
Inspiration
Thanks
License

MIT License © dyaa

Keywords

FAQs

Package last updated on 30 Jan 2019

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