Implement timeout and cancellation on top of node-fetch or standard fetch.
npm install @altipla/fetch-timeout
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.
import { fetchTimeout } from '@altipla/fetch-timeout'
let reply = await fetchTimeout('https://www.example.com/', {
method: 'POST',
body: '...',
headers: { ... },
})
import { fetchTimeout } from '@altipla/fetch-timeout'
let reply = await fetchTimeout('https://www.example.com/', {
method: 'POST',
body: '...',
headers: { ... },
timeout: 30000, // milliseconds
})
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,
})
import { fetchTimeout } from '@altipla/fetch-timeout'
try {
let reply = await fetchTimeout('https://www.example.com/', {
timeout: 5000,
})
} catch (err: any) {
if (err.name === 'AbortError') {
...
}
}