1.1.0 • Published 9 months ago
@guoyunhe/retry v1.1.0
@guoyunhe/retry
a better retry function
Installation
npm install --save @guoyunhe/retry
Examples
import { retry } from '@guoyunhe/retry';
// By default, it will retry max. 5 times on any error.
await retry(() => fetch('/foobar.json'));
// You can define a different max. retry limit.
await retry(() => fetch('/foobar.json'), { retries: 666 });
// You can also control whether to retry by checking error type or message.
await retry(() => fetch('/foobar.json'), {
shouldRetry: (e) => e.name === 'TypeError' && e.message === 'Failed to fetch',
});
// Retrying too often can crash systems. Use retryDelay to reduce retry frequence.
await retry(() => fetch('/foobar.json'), {
// retryIndex starts from 1, not 0
retryDelay: (retryIndex) => 1000 * Math.pow(retryIndex, 2);
});
Comparsion
Package | TS | ESM | Promise | Bundle Size |
---|---|---|---|---|
@guoyunhe/retry | ✅ | ✅ | ✅ | |
@humanwhocodes/retry | ✅ | ✅ | ✅ | |
p-retry | ✅ | ✅ | ✅ | |
async-retry | ✅ | ❌ | ✅ | |
retry | ✅ | ❌ | ❌ |