Files
nodeMap/services/api/fetchWithTimeout.js
2025-03-05 09:48:02 +01:00

17 lines
421 B
JavaScript

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;