Attach a timeout to any promise, if the timeout resolves first ignore the original promise and throw an error
The promise to attach a timeout to
The options to use
Optional
An AbortController to abort the original promise
The number of milliseconds to wait before timing out
The result of the promise
TimeoutError If the timeout resolves first
try { let result = await timeout( fetch("https://example.com"), { ms: 100 } );} catch (error) { if (error instanceof TimeoutError) { // Handle timeout }} Copy
try { let result = await timeout( fetch("https://example.com"), { ms: 100 } );} catch (error) { if (error instanceof TimeoutError) { // Handle timeout }}
try { let controller = new AbortController(); let result = await timeout( fetch("https://example.com", { signal: controller.signal }), { ms: 100, controller } );} catch (error) { if (error instanceof TimeoutError) { // Handle timeout }} Copy
try { let controller = new AbortController(); let result = await timeout( fetch("https://example.com", { signal: controller.signal }), { ms: 100, controller } );} catch (error) { if (error instanceof TimeoutError) { // Handle timeout }}
Attach a timeout to any promise, if the timeout resolves first ignore the original promise and throw an error