Socket
Socket
Sign inDemoInstall

@jsier/retrier

Package Overview
Dependencies
0
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @jsier/retrier

Promise based retry logic.


Version published
Weekly downloads
1.5K
decreased by-6.23%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

@jsier/retrier

Promise based retry logic

A simple, efficient and lightweight package without external dependencies which helps quickly implement JavaScript promise based retry logic. Retrier class is built with TypeScript (preserves full compatibility with pure JavaScript) and exposes intuitive and easy-to-use API.

Supports:

  • First attempt delay
  • Delay between attempts
  • Limiting number of attempts
  • Callback to stop retrying if some condition is met (e.g. specific error is encountered)
  • Callback to keep retrying if some condition is met (e.g. resolved value is unsatisfactory)

Getting Started

$ npm install @jsier/retrier --save

Usage

Retrier constructor as a first argument expects function which returns a promise. Second argument is optional and expects retry options object.

import { Retrier } from '@jsier/retrier';

const options = { limit: 5, delay: 2000 };
const retrier = new Retrier(options);
retrier
  .resolve(attempt => new Promise((resolve, reject) => reject('Rejected!')))
  .then(
    result => console.log(result),
    error => console.error(error) // After 5 attempts logs: "Rejected!"
  );

Retry Options

By default, the retrier will retry until provided promise resolves successfully or until retry limit is reached. To override the defaults please see retry options below:

PropertyDescriptionTypeDefault
limitNumber of attempts.number1
delayDelay between attempts in milliseconds.number0
firstAttemptDelayDelay first attempt.number0
keepRetryingIfTreat resolved value as invalid and keep retrying - if provided function returns truthy value. Example: keepRetryingIf: (response, attempt) => response.status === 202;Functionundefined
stopRetryingIfStop retrying (reject) if specific error - (provided function returns truthy value). Example: stopRetryingIf: (error, attempt) => error.status === 500;Functionundefined

Support

All suggestions and improvements are welcomed and appreciated.

License

The MIT License.

Keywords

FAQs

Last updated on 23 Feb 2020

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