Socket
Socket
Sign inDemoInstall

ts-retry-promise

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-retry-promise

retry for functions returning a promise


Version published
Weekly downloads
517K
increased by5.32%
Maintainers
1
Weekly downloads
 
Created

What is ts-retry-promise?

The ts-retry-promise npm package provides a way to retry promises with customizable options. It is useful for handling transient errors in asynchronous operations by retrying the operation until it succeeds or a maximum number of retries is reached.

What are ts-retry-promise's main functionalities?

Basic Retry

This feature allows you to retry a promise-based function a specified number of times. In this example, the fetchData function simulates an API call that may fail. The retry function will attempt to call fetchData up to 3 times before giving up.

const retry = require('ts-retry-promise');

const fetchData = () => {
  return new Promise((resolve, reject) => {
    // Simulate an API call that may fail
    if (Math.random() > 0.5) {
      resolve('Data fetched successfully');
    } else {
      reject('Fetch failed');
    }
  });
};

retry(fetchData, { retries: 3 })
  .then(result => console.log(result))
  .catch(err => console.error(err));

Exponential Backoff

This feature allows you to retry a promise-based function with exponential backoff. The delay between retries increases exponentially. In this example, the initial delay is 1000ms and it doubles with each retry.

const retry = require('ts-retry-promise');

const fetchData = () => {
  return new Promise((resolve, reject) => {
    // Simulate an API call that may fail
    if (Math.random() > 0.5) {
      resolve('Data fetched successfully');
    } else {
      reject('Fetch failed');
    }
  });
};

retry(fetchData, { retries: 3, delay: 1000, factor: 2 })
  .then(result => console.log(result))
  .catch(err => console.error(err));

Custom Retry Logic

This feature allows you to define custom logic to determine if a promise should be retried. In this example, the shouldRetry function checks if the error message is 'Fetch failed' to decide whether to retry the fetchData function.

const retry = require('ts-retry-promise');

const fetchData = () => {
  return new Promise((resolve, reject) => {
    // Simulate an API call that may fail
    if (Math.random() > 0.5) {
      resolve('Data fetched successfully');
    } else {
      reject('Fetch failed');
    }
  });
};

const shouldRetry = (err) => {
  // Custom logic to determine if the error is retryable
  return err === 'Fetch failed';
};

retry(fetchData, { retries: 3, shouldRetry })
  .then(result => console.log(result))
  .catch(err => console.error(err));

Other packages similar to ts-retry-promise

Keywords

FAQs

Package last updated on 29 Aug 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