@bybrave/promise-retry2
v3.0.1
Published
Maintained promise-retry fork: zero runtime dependencies, ESM + CommonJS, built-in TypeScript types, and the retry operation exposed for reset/stop. Drop-in for promise-retry.
Maintainers
Readme
@bybrave/promise-retry2
Maintained, drop-in fork of promise-retry — retries a function that returns a promise, with exponential backoff.
The original has had no release since 2020 (2.0.1) while still pulling ~188M downloads/month, with types living in a separate @types/promise-retry (~1.1M/month) and two runtime dependencies. This fork bundles the TypeScript types, drops to zero runtime dependencies, ships ESM + CommonJS, and exposes the retry operation so you can reset the backoff or stop pending retries. The API is unchanged.
npm install @bybrave/promise-retry2const promiseRetry = require('@bybrave/promise-retry2'); // CommonJS
import promiseRetry from '@bybrave/promise-retry2'; // ESMUsage
promiseRetry((retry, number) => {
return doSomething().catch(retry);
}, {retries: 5});fn is called with (retry, attemptNumber, operation). Call retry(err) to trigger another attempt; it always throws, so returning/throwing it ends the current try. When the retries run out, the promise rejects with the last error. Options are the same as the underlying retry module (retries, factor, minTimeout, maxTimeout, randomize, forever). The alternate (options, fn) argument order also works.
What's fixed
| Issue | Problem | Fix |
|---|---|---|
| — | Types shipped separately as @types/promise-retry. | Types are bundled — no extra install. |
| — | Two runtime dependencies (retry, err-code). | Zero runtime dependencies — both vendored in. |
| — | CommonJS only, no import. | Dual ESM + CommonJS build. |
| #25, #32 | No way to reset the backoff when a dependency recovers early, or to inspect/stop the operation. | The retry operation is exposed as the third argument — call operation.reset(), operation.stop(), or operation.getTimeout(). |
| #37 | No CHANGELOG / release notes. | Releases are documented. |
Exposing the operation
promiseRetry((retry, number, operation) => {
return call().catch((err) => {
// When the network is back, forget the accumulated backoff delay:
if (isBackOnline()) operation.reset();
retry(err);
});
}, {retries: 10});operation.getTimeout() returns the ms until the next scheduled attempt — handy for logging "retrying in 16s".
Notes on behavior (unchanged, by design)
- Memory with huge
retries(#20): the underlyingretrymodule precomputes a timeouts array of lengthretries, so memory grows O(retries). For effectively-infinite retries useforever: true(or a modestretrieswith a largemaxTimeout) instead of a giant number. retries: 0(#8) means "no retries", not "retry forever" — useforever: truefor that.- After calling
retry(err), don't throw again in the same tick without returning it —return retry(err)(#14).
Migration from promise-retry
Replace the dependency and the import, and remove @types/promise-retry from your devDependencies — the types are built in. Everything behaves the same, plus the fixes above.
Support
If this package saves you time, you can support maintenance:
Bitcoin (BTC): bc1q37557q5jpeaxqydzwvf3jgj7zhnfpn2td3q40q
Credits & license
MIT, same as the original — see LICENSE. Based on node-promise-retry by IndigoUnited.
