Gathering detailed insights and metrics for ts-retryable
Gathering detailed insights and metrics for ts-retryable
Gathering detailed insights and metrics for ts-retryable
Gathering detailed insights and metrics for ts-retryable
npm install ts-retryable
Typescript
Module System
Node Version
NPM Version
71.1
Supply Chain
90.5
Quality
75.5
Maintenance
100
Vulnerability
81.3
License
TypeScript (100%)
Total Downloads
1,094
Last Day
1
Last Week
4
Last Month
11
Last Year
189
MPL-2.0 License
2 Stars
52 Commits
1 Watchers
1 Branches
1 Contributors
Updated on May 09, 2025
Minified
Minified + Gzipped
Latest Version
0.9.6
Package Id
ts-retryable@0.9.6
Unpacked Size
16.81 kB
Size
3.73 kB
File Count
11
NPM Version
9.8.0
Node Version
20.5.0
Published on
Sep 04, 2023
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
300%
4
Compared to previous week
Last Month
-66.7%
11
Compared to previous month
Last Year
-79.1%
189
Compared to previous year
Given a function, creates a new function with built-in retry capabilities.
1const lessFlaky = retryable(flaky, {shouldRetry: () => true})
1import { retryable } from 'ts-retryable' 2import nodeFetch from "node-fetch"; 3 4export const myFetch = retryable(nodeFetch, { 5 shouldRetry: ((count, error) => count < 3), 6 delay: 5000 7}) 8// Elsewhere, use this `myFetch` but get automatic retries.
In my code, the shouldRetry
became fairly sophisticated, analyzing not only the retry count, but the details of the error that was thrown.
This will work with any async function, and I developed for an API-heavy app. Dropbox was one of the APIs, and here's more realistic, but simplified example:
1// import dropboxApi ...
2dropboxApi.filesDownload = makeRetryable(
3 dropboxApi.filesDownload,
4 {
5 shouldRetry: async function (count: number, e: DropboxResponse<files.FileMetadata>): Promise<boolean> {
6 if (count >= 4) return false
7 if (e.status === 401) {
8 await client.refreshAccessToken()
9 return true
10 }
11 return isNetworkError(e);
12 },
13 delay: 5000,
14 })
I also used functional composition to produce a fetch
that handled OAuth and network-induced errors.
The retry capabilities are controlled by the options object provided:
shouldRetry
is a function you provide that determines whether a
failure will retry. It receives the retry count (starting at 1), and
the exception that was thrown/rejected, and returns true or false.
Here are common examples:
1() => true // just keep trying (forever?) 2(c) => c <= 5 // 5 or fewer times 3(_, e) => e.status === 503 // on a specific error
Note: Although this function is basically function asking for true/false, it is possible to execute other logic, including side effects. This could be any sort of "extra" logic to prepare for a retry.
delay
is how long to wait before retrying. By default, there is no delay. The value of delay is either a number (milliseconds) or a function.
The function will receive the call count and exception thrown. Exponential backoff looks like (c) => Math.pow(2, c) * 1000
. It's possible that the exception would be useful: the exception saying a server is busy may suggest a longer timeout than some intermittent exception.
This module assumes that the function will return a promise, as this will be 90% (or more) of the use cases. If you want to retry synchronous code, use retryableSync
instead. It does not support any sort of delay, and shouldRetry
should be sync as well. Failure for sync functions means throwing an exception, (and
failure for Promise/async functions means returning a rejected promise).
Lots of these solutions are (1) not Typescript (2) function specific, such as fetch
only. Feel free to try any of them.
No vulnerabilities found.
No security vulnerabilities found.