2.0.7 • Published 7 months ago

retry-lib v2.0.7

Weekly downloads
-
License
ISC
Repository
github
Last release
7 months ago

retry-lib

A javascript and typescript helper that runs async await function several times with delays until it returns result or attempts ends.

Installation

npm install retry-lib --save

Usage

Import

TypeScript

import retry from 'retry-lib';

Javascript

var retry = require('retry-lib'); // you can use commonjs
// or
var { default: retry } = require('retry-lib');
// or
import retry from 'retry-lib'; // you can use esm modules

Example

const methodWithError = async (parameter) => {
    if (methodWithError.counter < 3) {
        methodWithError.counter++;
        throw new Error('Error' + methodWithError.counter);
    }

    return parameter;
};
methodWithError.counter = 1;

const result = await retry({
    tryCounter: 3,
    delayBetweenRetryMs: 200,
    factor: 2, // second retry after 400ms (2 * 200), third retry after 600ms (3 * 200), and ...
    func: async () => {
        return methodWithError('finish1');
    },
});

console.log(result); // output 'finish1'

Parameters

interface IRetryParams {
    tryCounter: number;
    delayBetweenRetryMs: number;
    factor: number;
    func: () => Promise<any>;
    onError?: (error: any): Promise<boolean>;;
}

export declare function retry(params: IRetryParams): Promise<any>;
2.0.7

7 months ago

2.0.6

7 months ago

2.0.5

7 months ago

2.0.4

7 months ago

2.0.3

7 months ago

2.0.1

7 months ago

2.0.0

7 months ago