timeout.ts
This file
Path
source/utils/timeout.ts
32 lines · 684 bytes · TypeScript
Last changed by
Fix support with environments that add support for custom `fetch` options (#536)Kelvin Del Montebefore adoptione93bc6d
History of this fileTake it
Fix abortions between retries (#433)Richie Bendallbefore adoptiondddf7ba
lines 1–10why, one step back1import {TimeoutError} from '../errors/TimeoutError.js';23export type TimeoutOptions = {4 timeout: number;5 fetch: typeof fetch;6};78// `Promise.race()` workaround (#91)9export default async function timeout(10 request: Request,Fix support with environments that add support for custom `fetch` options (#536)Kelvin Del Montebefore adoptione93bc6d
line 11why, one step back11 init: RequestInit,Fix abortions between retries (#433)Richie Bendallbefore adoptiondddf7ba
lines 12–24why, one step back12 abortController: AbortController | undefined,13 options: TimeoutOptions,14): Promise<Response> {15 return new Promise((resolve, reject) => {16 const timeoutId = setTimeout(() => {17 if (abortController) {18 abortController.abort();19 }2021 reject(new TimeoutError(request));22 }, options.timeout);2324 void optionsFix support with environments that add support for custom `fetch` options (#536)Kelvin Del Montebefore adoptione93bc6d
line 25why, one step back25 .fetch(request, init)Fix abortions between retries (#433)Richie Bendallbefore adoptiondddf7ba
lines 26–32why, one step back26 .then(resolve)27 .catch(reject)28 .then(() => {29 clearTimeout(timeoutId);30 });31 });32}