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

@lifeomic/attempt

Package Overview
Dependencies
Maintainers
2
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@lifeomic/attempt

Library that can be used to retry functions that return promise

  • 2.0.7
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
104K
decreased by-10.21%
Maintainers
2
Weekly downloads
 
Created

What is @lifeomic/attempt?

@lifeomic/attempt is an npm package designed to handle retry logic for asynchronous operations. It provides a simple and flexible way to retry operations that may fail due to transient issues, such as network errors or temporary unavailability of a service.

What are @lifeomic/attempt's main functionalities?

Basic Retry

This feature allows you to retry an asynchronous operation a specified number of times with a delay between attempts. In this example, the operation is retried up to 5 times with a 1-second delay between attempts.

const attempt = require('@lifeomic/attempt');

async function fetchData() {
  return attempt(async () => {
    // Simulate an operation that may fail
    if (Math.random() < 0.5) {
      throw new Error('Random failure');
    }
    return 'Success';
  }, {
    maxAttempts: 5,
    delay: 1000
  });
}

fetchData().then(console.log).catch(console.error);

Exponential Backoff

This feature allows you to implement exponential backoff for retrying an operation. The delay between attempts increases exponentially. In this example, the delay starts at 1 second and doubles with each subsequent attempt.

const attempt = require('@lifeomic/attempt');

async function fetchData() {
  return attempt(async () => {
    // Simulate an operation that may fail
    if (Math.random() < 0.5) {
      throw new Error('Random failure');
    }
    return 'Success';
  }, {
    maxAttempts: 5,
    delay: 1000,
    factor: 2
  });
}

fetchData().then(console.log).catch(console.error);

Custom Retry Logic

This feature allows you to define custom logic for handling errors and deciding whether to continue retrying. In this example, a custom error handler logs the error message and continues retrying.

const attempt = require('@lifeomic/attempt');

async function fetchData() {
  return attempt(async () => {
    // Simulate an operation that may fail
    if (Math.random() < 0.5) {
      throw new Error('Random failure');
    }
    return 'Success';
  }, {
    maxAttempts: 5,
    delay: 1000,
    handleError: (err, context) => {
      console.log(`Attempt ${context.attemptNumber} failed: ${err.message}`);
      return true; // Continue retrying
    }
  });
}

fetchData().then(console.log).catch(console.error);

Other packages similar to @lifeomic/attempt

Keywords

FAQs

Package last updated on 22 Oct 2021

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