wing-async-retry
TypeScript icon, indicating that this package has built-in type declarations

0.0.4 • Public • Published

wing-async-retry

Install

$ npm install wing-async-retry

Usage

import { runFlatWithLimitedTimeout, runFlatWithLimitedTimes, runWithLimitedTimeout, runWithLimitedTimes } from 'wing-async-retry';

// Or
<script src='/path/to/wing-async-retry/wing-async-retry.js'></script>;
const { runFlatWithLimitedTimeout, runFlatWithLimitedTimes, runWithLimitedTimeout, runWithLimitedTimes } = $WingAsyncRetry;

function getData(n = 99): Promise<string> {
  return new Promise((resolve, reject) => {
    const random = Math.random();
    setTimeout(() => (random > 0.6995 ? resolve(`OK_${n}`) : reject(`Error_${n}`)), random * 800);
  });
}

async function test() {
  for (let i = 1; i < 3; i++) {
    try {
      const data = await runWithLimitedTimes(getData, { debug: true, retryTimes: 5 });
      console.log('runWithLimitedTimes - Data: ', data);
    } catch (error) {
      console.log('runWithLimitedTimes - Error: ', error);
    }

    const result1 = await runFlatWithLimitedTimes(() => getData(i * 10), { debug: true, retryDelay: 1000 });
    console.log('runWithLimitedTimes - Flat Data', result1);

    try {
      const data = await runWithLimitedTimeout(() => getData(i * 20), { debug: true, retryDelay: 1200 });
      console.log('runWithLimitedTimes - Data: ', data);
    } catch (error) {
      console.log('runWithLimitedTimes - Error: ', error);
    }

    const result2 = await runFlatWithLimitedTimeout(getData, { debug: true, retryTimeout: 6500 });
    console.log('runWithLimitedTimes - Flat Data', result2);

    console.log(i + '-'.repeat(50));
  }
}

Methods

runWithLimitedTimes(AsyncFn[, RunWithLimitedTimesOptions])

Retry a limited number of times. Catch exception or error by try-catch

runFlatWithLimitedTimes(AsyncFn[, RunWithLimitedTimesOptions])

Retry a limited number of times. catch exception or error by [Error/Exception, Data] flat-format result.

runWithLimitedTimeout(AsyncFn[, RunWithLimitedTimeoutOptions])

Retry with limited time. Catch exception or error by try-catch

runFlatWithLimitedTimeout(AsyncFn[, RunWithLimitedTimeoutOptions])

Retry with limited time. catch exception or error by [Error/Exception, Data] flat-format result.

Types

AsyncFn

asynchronous functions. such as promises

RunWithLimitedTimesOptions

Property Type Description Required Default
times Number The number of times to retry No 3
delay Number The delay of each retry, in ms No 4
debug Boolean Whether to enable log output No false

RunWithLimitedTimeoutOptions

Property Type Description Required Default
timeout Number The timeout that needs to be retried, in ms No 5000
delay Number The delay of each retry, in ms No 4
debug Boolean Whether to enable log output No false

Readme

Keywords

Package Sidebar

Install

npm i wing-async-retry

Weekly Downloads

2

Version

0.0.4

License

MIT

Unpacked Size

32.4 kB

Total Files

9

Last publish

Collaborators

  • wadlay