timeout.ts
This file
Path
source/utils/timeout.ts
46 lines · 1.4 KiB · TypeScript
Last changed by
RefactorSindre Sorhusbefore adoptiona5ed57b
History of this fileTake it
The walk stopped after 200 commits: lines older than 37fc250 point at it.
Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
line 1why, one step back1import {TimeoutError} from '../errors/TimeoutError.js';Type custom fetch callbacks with Request inputsSindre Sorhusbefore adoptione58817d
line 2why, one step back2import type {InternalOptions} from '../types/options.js';RefactorSindre Sorhusbefore adoptiona5ed57b
line 3why, one step back3import {cancelBody} from './body.js';Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
lines 4–6why, one step back45export type TimeoutOptions = {6 timeout: number;Type custom fetch callbacks with Request inputsSindre Sorhusbefore adoptione58817d
line 7why, one step back7 fetch: InternalOptions['fetch'];Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
lines 8–16why, one step back8};910// `Promise.race()` workaround (#91)11export default async function timeout(12 request: Request,13 init: RequestInit,14 abortController: AbortController | undefined,15 options: TimeoutOptions,16): Promise<Response> {Fix a stale timeout timer aborting the retried attempt when a custom `fetch` throws synchronouslySindre Sorhusbefore adoption0f2e281
line 17why, one step back17 let timeoutId: ReturnType<typeof setTimeout> | undefined;Release the bodies a caller cannot reach and stop sharing search parametersSindre Sorhusbefore adoptionec3d17f
line 18why, one step back18 let timedOut = false;Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
line 19why, one step back19Fix a stale timeout timer aborting the retried attempt when a custom `fetch` throws synchronouslySindre Sorhusbefore adoption0f2e281
lines 20–22why, one step back20 try {21 return await new Promise((resolve, reject) => {22 timeoutId = setTimeout(() => {Release the bodies a caller cannot reach and stop sharing search parametersSindre Sorhusbefore adoptionec3d17f
lines 23–24why, one step back23 timedOut = true;24RefactorSindre Sorhusbefore adoptiona5ed57b
line 25why, one step back25 abortController?.abort();Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
line 26why, one step back26Fix a stale timeout timer aborting the retried attempt when a custom `fetch` throws synchronouslySindre Sorhusbefore adoption0f2e281
lines 27–29why, one step back27 reject(new TimeoutError(request));28 }, options.timeout);29Fix `fetch: window.fetch` throwing "Illegal invocation" in browsersSindre Sorhusbefore adoption92ef60e
line 30why, one step back30 // Called unbound so a native `window.fetch` is not invoked with `options` as `this`, which throws "Illegal invocation" in browsers.Fix a stale timeout timer aborting the retried attempt when a custom `fetch` throws synchronouslySindre Sorhusbefore adoption0f2e281
line 31why, one step back31 // A synchronous throw rejects the promise, and `finally` still clears the timer so it cannot abort a later retry attempt.Fix `fetch: window.fetch` throwing "Illegal invocation" in browsersSindre Sorhusbefore adoption92ef60e
line 32why, one step back32 const {fetch} = options;Release the bodies a caller cannot reach and stop sharing search parametersSindre Sorhusbefore adoptionec3d17f
lines 33–35why, one step back33 fetch(request, init).then(response => {34 // A response arriving after the timeout already won is discarded, so its unused body is cancelled. Fire-and-forget: cancellation failures are ignored and never delay the rejection.35 if (timedOut) {RefactorSindre Sorhusbefore adoptiona5ed57b
line 36why, one step back36 cancelBody(response);Release the bodies a caller cannot reach and stop sharing search parametersSindre Sorhusbefore adoptionec3d17f
lines 37–41why, one step back37 return;38 }3940 resolve(response);41 }).catch(reject);Fix a stale timeout timer aborting the retried attempt when a custom `fetch` throws synchronouslySindre Sorhusbefore adoption0f2e281
lines 42–45why, one step back42 });43 } finally {44 clearTimeout(timeoutId);45 }Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
line 46why, one step back46}