with-auto-retry
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

with-auto-retry

npm version build

Creates a function that will automaticly retry serval times on fail. Support both sync and async functions.

example:

import withAutoRetry from 'with-auto-retry';
// or js
// const withAutoRetry = require('with-auto-retry').default;

let tryCount = 0;
const func = () => {
    if (tryCount < 3) {
        tryCount++;
        throw new Error('This is a random error.');
    }
    return tryCount;
};
const newFunc = withAutoRetry(func);
const value = newFunc();
console.log(value); // value is 3 and no exeptions

usage:

withAutoRetry(func);
// or customize
const configs = { retryTime: 3 };
withAutoRetry(func, configs);

configs are all optional:

interface Configs {
    /** How many times to retry before throw exception, defaults to 5 */
    retryTime?: number;
    /** Invoke before retry, return false to stop retring. retryCount start with 1 */
    retryPredicater?: (retryCount: number, e: any) => boolean;
    /** Seconds to wait before retry, defaults to 0. Func should be async to use this feature */
    secondsBeforeRetry?: number;
}

Readme

Keywords

none

Package Sidebar

Install

npm i with-auto-retry

Weekly Downloads

5

Version

1.0.2

License

MIT

Unpacked Size

12.3 kB

Total Files

10

Last publish

Collaborators

  • wangsijie