Coder Social home page Coder Social logo

gtbl2012 / typescript-retry-decorator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vcfvct/typescript-retry-decorator

0.0 0.0 0.0 29 KB

lightweight typescript retry decorator with 0 dependency.

License: MIT License

TypeScript 88.66% JavaScript 11.34%

typescript-retry-decorator's Introduction

Retry

A simple retry decorator for typescript with 0 dependency.

This is inspired by the Spring-Retry project.

Import and use it. Retry for Promise is supported as long as the runtime has promise(nodejs/evergreen-browser).

Install

npm install typescript-retry-decorator

Options

Option Name Type Required? Default Description
maxAttempts number Yes - The max attempts to try
backOff number No 0 number in ms to back off. If not set, then no wait
backOffPolicy enum No FixedBackOffPolicy can be fixed or exponential
exponentialOption object No { maxInterval: 2000, multiplier: 2 } This is for the ExponentialBackOffPolicy
The max interval each wait and the multiplier for the backOff.
doRetry (e: any) => boolean No - Function with error parameter to decide if repetition is necessary.

Example

import { Retryable, BackOffPolicy } from 'typescript-retry-decorator';

let count: number = 1;

class RetryExample {
  @Retryable({ maxAttempts: 3 })
  static async noDelayRetry() {
    console.info(`Calling noDelayRetry for the ${count++} time at ${new Date().toLocaleTimeString()}`);
    throw new Error('I failed!');
  }

  @Retryable({ 
    maxAttempts: 3,
    backOff: 1000,
    doRetry: (e: Error) => {
      return e.message === 'Error: 429';
    }
   })
  static async doRetry() {
    console.info(`Calling doRetry for the ${count++} time at ${new Date().toLocaleTimeString()}`);
    throw new Error('Error: 429');
  }

  @Retryable({ 
    maxAttempts: 3,
    backOff: 1000,
    doRetry: (e: Error) => {
      return e.message === 'Error: 429';
    }
   })
  static async doNotRetry() {
    console.info(`Calling doNotRetry for the ${count++} time at ${new Date().toLocaleTimeString()}`);
    throw new Error('Error: 404');
  }

  @Retryable({
    maxAttempts: 3,
    backOffPolicy: BackOffPolicy.FixedBackOffPolicy,
    backOff: 1000
  })
  static async fixedBackOffRetry() {
    console.info(`Calling fixedBackOffRetry 1s for the ${count++} time at ${new Date().toLocaleTimeString()}`);
    throw new Error('I failed!');
  }

  @Retryable({
    maxAttempts: 3,
    backOffPolicy: BackOffPolicy.ExponentialBackOffPolicy,
    backOff: 1000,
    exponentialOption: { maxInterval: 4000, multiplier: 3 }
  })
  static async ExponentialBackOffRetry() {
    console.info(`Calling ExponentialBackOffRetry backOff 1s, multiplier=3 for the ${count++} time at ${new Date().toLocaleTimeString()}`);
    throw new Error('I failed!');
  }
}

(async () => {
  try {
    resetCount();
    await RetryExample.noDelayRetry();
  } catch (e) {
    console.info(`All retry done as expected, final message: '${e.message}'`);
  }

  try {
    resetCount();
    await RetryExample.doRetry();
  } catch (e) {
    console.info(`All retry done as expected, final message: '${e.message}'`);
  }

  try {
    resetCount();
    await RetryExample.doNotRetry();
  } catch (e) {
    console.info(`All retry done as expected, final message: '${e.message}'`);
  }

  try {
    resetCount();
    await RetryExample.fixedBackOffRetry();
  } catch (e) {
    console.info(`All retry done as expected, final message: '${e.message}'`);
  }

  try {
    resetCount();
    await RetryExample.ExponentialBackOffRetry();
  } catch (e) {
    console.info(`All retry done as expected, final message: '${e.message}'`);
  }
  
})();

function resetCount() {
  count = 1;
}

Run the above code with ts-node, then output will be:

Calling noDelayRetry for the 1 time at 10:01:53 PM
Calling noDelayRetry for the 2 time at 10:01:53 PM
Calling noDelayRetry for the 3 time at 10:01:53 PM
Calling noDelayRetry for the 4 time at 10:01:53 PM
All retry done as expected, final message: 'Failed for 'noDelayRetry' for 3 times.'

Calling doRetry for the 1 time at 10:01:53 PM
Calling doRetry for the 2 time at 10:01:54 PM
Calling doRetry for the 3 time at 10:01:55 PM
Calling doRetry for the 4 time at 10:01:56 PM
All retry done as expected, final message: 'Failed for 'doRetry' for 3 times.'

Calling doNotRetry for the 1 time at 10:01:56 PM
All retry done as expected, final message: 'Error: 404'

Calling fixedBackOffRetry 1s for the 1 time at 10:01:56 PM
Calling fixedBackOffRetry 1s for the 2 time at 10:01:57 PM
Calling fixedBackOffRetry 1s for the 3 time at 10:01:58 PM
Calling fixedBackOffRetry 1s for the 4 time at 10:01:59 PM
All retry done as expected, final message: 'Failed for 'fixedBackOffRetry' for 3 times.'

Calling ExponentialBackOffRetry backOff 1s, multiplier=3 for the 1 time at 10:01:59 PM
Calling ExponentialBackOffRetry backOff 1s, multiplier=3 for the 2 time at 10:02:00 PM
Calling ExponentialBackOffRetry backOff 1s, multiplier=3 for the 3 time at 10:02:03 PM
Calling ExponentialBackOffRetry backOff 1s, multiplier=3 for the 4 time at 10:02:07 PM
All retry done as expected, final message: 'Failed for 'ExponentialBackOffRetry' for 3 times.'

typescript-retry-decorator's People

Contributors

dependabot[bot] avatar mananruck avatar vcfvct avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.