forked from davidflanagan/jstdg7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchWithTimeout.js
16 lines (16 loc) · 870 Bytes
/
fetchWithTimeout.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// This function is like fetch(), but it adds support for a timeout
// property in the options object and aborts the fetch if it is not complete
// within the number of milliseconds specified by that property.
function fetchWithTimeout(url, options={}) {
if (options.timeout) { // If the timeout property exists and is nonzero
let controller = new AbortController(); // Create a controller
options.signal = controller.signal; // Set the signal property
// Start a timer that will send the abort signal after the specified
// number of milliseconds have passed. Note that we never cancel
// this timer. Calling abort() after the fetch is complete has
// no effect.
setTimeout(() => { controller.abort(); }, options.timeout);
}
// Now just perform a normal fetch
return fetch(url, options);
}