You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
684 B
29 lines
684 B
import { memoize } from 'lodash';
|
|
|
|
const getFetchWithTimeout = memoize((timeout) => {
|
|
if (!Number.isInteger(timeout) || timeout < 1) {
|
|
throw new Error('Must specify positive integer timeout.');
|
|
}
|
|
|
|
return async function _fetch(url, opts) {
|
|
const abortController = new window.AbortController();
|
|
const { signal } = abortController;
|
|
const f = window.fetch(url, {
|
|
...opts,
|
|
signal,
|
|
});
|
|
|
|
const timer = setTimeout(() => abortController.abort(), timeout);
|
|
|
|
try {
|
|
const res = await f;
|
|
clearTimeout(timer);
|
|
return res;
|
|
} catch (e) {
|
|
clearTimeout(timer);
|
|
throw e;
|
|
}
|
|
};
|
|
});
|
|
|
|
export default getFetchWithTimeout;
|
|
|