Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/altipla-consulting/fetch-timeout
Implement timeout and cancellation on top of node-fetch or standard fetch.
https://github.com/altipla-consulting/fetch-timeout
Last synced: about 1 month ago
JSON representation
Implement timeout and cancellation on top of node-fetch or standard fetch.
- Host: GitHub
- URL: https://github.com/altipla-consulting/fetch-timeout
- Owner: altipla-consulting
- License: mit
- Created: 2023-04-08T17:48:09.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-11-06T14:49:08.000Z (about 1 year ago)
- Last Synced: 2024-08-08T19:57:28.057Z (5 months ago)
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@altipla/fetch-timeout
- Size: 40 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fetch-timeout
Implement timeout and cancellation on top of node-fetch or standard fetch.
## Install
```sh
npm install @altipla/fetch-timeout
```## Usage
Replace any call to `fetch` with our helper `fetchTimeout`. Types are compatible as we only add a new option `timeout` that can be added when needed.
### With no timeout (normal fetch)
```ts
import { fetchTimeout } from '@altipla/fetch-timeout'let reply = await fetchTimeout('https://www.example.com/', {
method: 'POST',
body: '...',
headers: { ... },
})
```### With a timeout
```ts
import { fetchTimeout } from '@altipla/fetch-timeout'let reply = await fetchTimeout('https://www.example.com/', {
method: 'POST',
body: '...',
headers: { ... },
timeout: 30000, // milliseconds
})
```### With a timeout and a cancel signal combined
```ts
import { fetchTimeout } from '@altipla/fetch-timeout'let ctrl = new AbortController()
abortButton.addEventListener('click', () => ctrl.abort())
let reply = await fetchTimeout('https://www.example.com/', {
method: 'POST',
body: '...',
headers: { ... },
timeout: 30000,
signal: controller.signal,
})
```### Check if there was a timeout
```ts
import { fetchTimeout } from '@altipla/fetch-timeout'try {
let reply = await fetchTimeout('https://www.example.com/', {
timeout: 5000,
})
} catch (err: any) {
if (err.name === 'AbortError') {
...
}
}
```