19 lines
458 B
JavaScript
19 lines
458 B
JavaScript
// services/api/fetchWithTimeout.js
|
|
|
|
const fetchWithTimeout = (url, options, timeout = 5000) => {
|
|
const controller = new AbortController();
|
|
const id = setTimeout(() => controller.abort(), timeout);
|
|
|
|
return fetch(url, { ...options, signal: controller.signal })
|
|
.then((response) => {
|
|
clearTimeout(id);
|
|
return response;
|
|
})
|
|
.catch((error) => {
|
|
clearTimeout(id);
|
|
throw error;
|
|
});
|
|
};
|
|
|
|
export default fetchWithTimeout;
|