All Projects → vcfvct → typescript-retry-decorator

vcfvct / typescript-retry-decorator

Licence: MIT license
lightweight typescript retry decorator with 0 dependency.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to typescript-retry-decorator

DRAM
Distilled and Refined Annotation of Metabolism: A tool for the annotation and curation of function for microbial and viral genomes
Stars: ✭ 159 (+218%)
Mutual labels:  annotation
jest-retry
Jest retry pattern for flaky E2E tests
Stars: ✭ 36 (-28%)
Mutual labels:  retry
bioportal web ui
A Rails application for biological ontologies
Stars: ✭ 20 (-60%)
Mutual labels:  annotation
View-Load-ReTry
这个加载框架有点不一样,针对View进行加载,加载页面还保持了原View的属性,侧重点在灵活,哪里需要加载哪里,加载状态页面完全自定义,无任何限制,针对加载结果可以按需配置对应页面,LeakCanary检测无内存泄漏
Stars: ✭ 116 (+132%)
Mutual labels:  retry
ansible-docgen
Generate documentation from annotated Ansible Playbooks and Roles
Stars: ✭ 61 (+22%)
Mutual labels:  annotation
dart sealed
Dart and Flutter sealed class generator and annotations, with match methods and other utilities. There is also super_enum compatible API.
Stars: ✭ 16 (-68%)
Mutual labels:  annotation
min
A decorator web framework for deno
Stars: ✭ 21 (-58%)
Mutual labels:  decorator
vue-corator
this is vue decorator utils
Stars: ✭ 33 (-34%)
Mutual labels:  decorator
PhpSpecSkipExampleExtension
Skip your PhpSpec examples through annotations
Stars: ✭ 35 (-30%)
Mutual labels:  annotation
discord-nestjs
👾 NestJS package for discord.js
Stars: ✭ 173 (+246%)
Mutual labels:  decorator
fit
easy storage Object on SharedPreferences
Stars: ✭ 53 (+6%)
Mutual labels:  annotation
ngx-redux-core
The modern redux integration for Angular 6+
Stars: ✭ 32 (-36%)
Mutual labels:  decorator
aptk
A toolkit project to enable you to build annotation processors more easily
Stars: ✭ 28 (-44%)
Mutual labels:  annotation
frontend-toolkit
Tools, documentation and resources for creating front-end pages and apps in Hypothesis
Stars: ✭ 18 (-64%)
Mutual labels:  annotation
greek scansion
Python library for automatic analysis of Ancient Greek hexameter. The algorithm uses linguistic rules and finite-state technology.
Stars: ✭ 16 (-68%)
Mutual labels:  annotation
typescript-lazy-get-decorator
Lazily evaluates a getter on an object and caches the returned value
Stars: ✭ 33 (-34%)
Mutual labels:  decorator
MethodScope
Reduce repetitive inheritance works in OOP world using @MethodScope.
Stars: ✭ 33 (-34%)
Mutual labels:  annotation
KCommando
Annotation-based multifunctional command handler framework for JDA & Javacord.
Stars: ✭ 26 (-48%)
Mutual labels:  annotation
php-backoff
Simple back off / retry functionality
Stars: ✭ 24 (-52%)
Mutual labels:  retry
VIAN
No description or website provided.
Stars: ✭ 18 (-64%)
Mutual labels:  annotation

Retry

A simple retry decorator for typescript with 0 dependency.

This is inspired by the Spring-Retry project. Written in Typescript, 100% Test Coverage.

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.
value Error/Exception class No [ ] An array of Exception types that are retryable.

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, 
    value: [SyntaxError, ReferenceError]
  })
  static async noDelaySpecificRetry(): Promise<void> {
    console.info(`Calling noDelaySpecificRetry for the ${count++} time at ${new Date().toLocaleTimeString()}`);
    throw new SyntaxError('I failed with SyntaxError!');
  }

  @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 4:12:49 PM
Calling noDelayRetry for the 2 time at 4:12:49 PM
Calling noDelayRetry for the 3 time at 4:12:49 PM
Calling noDelayRetry for the 4 time at 4:12:49 PM
I failed!
All retry done as expected, final message: 'Failed for 'noDelayRetry' for 3 times.'
Calling noDelayRetry for the 1 time at 4:12:49 PM
Calling noDelayRetry for the 2 time at 4:12:49 PM
Calling noDelayRetry for the 3 time at 4:12:49 PM
Calling noDelayRetry for the 4 time at 4:12:49 PM
I failed with SyntaxError!
All retry done as expected, final message: 'Failed for 'noDelaySpecificRetry' for 3 times.'
Calling doRetry for the 1 time at 4:12:49 PM
Calling doRetry for the 2 time at 4:12:50 PM
Calling doRetry for the 3 time at 4:12:51 PM
Calling doRetry for the 4 time at 4:12:52 PM
Error: 429
All retry done as expected, final message: 'Failed for 'doRetry' for 3 times.'
Calling doNotRetry for the 1 time at 4:12:52 PM
All retry done as expected, final message: 'Error: 404'
Calling fixedBackOffRetry 1s for the 1 time at 4:12:52 PM
Calling fixedBackOffRetry 1s for the 2 time at 4:12:53 PM
Calling fixedBackOffRetry 1s for the 3 time at 4:12:54 PM
Calling fixedBackOffRetry 1s for the 4 time at 4:12:55 PM
I failed!
All retry done as expected, final message: 'Failed for 'fixedBackOffRetry' for 3 times.'
Calling ExponentialBackOffRetry backOff 1s, multiplier=3 for the 1 time at 4:12:55 PM
Calling ExponentialBackOffRetry backOff 1s, multiplier=3 for the 2 time at 4:12:56 PM
Calling ExponentialBackOffRetry backOff 1s, multiplier=3 for the 3 time at 4:12:59 PM
Calling ExponentialBackOffRetry backOff 1s, multiplier=3 for the 4 time at 4:13:03 PM
I failed!
All retry done as expected, final message: 'Failed for 'ExponentialBackOffRetry' for 3 times.'
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].