-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetchWithTimeout.js
24 lines (22 loc) · 968 Bytes
/
fetchWithTimeout.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function fetchWithTimeout(url, timeInMilliseconds) {
return new Promise((resolve, reject) => {
const controller = new AbortController(); // Create an AbortController
const signal = controller.signal; // Get the signal from the controller
const timer = setTimeout(() => {
controller.abort(); // Abort the fetch if timeout occurs
}, timeInMilliseconds);
fetch(url, { signal })
.then((res) => {
clearTimeout(timer); // Clear the timeout when fetch succeeds
resolve(res); // Resolve with the response
})
.catch((err) => {
clearTimeout(timer); // Clear the timeout in case of error
if (err.name === 'AbortError') {
reject(new Error('Request timed out')); // Handle abort error
} else {
reject(err); // Handle other errors
}
});
});
}