1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-24 02:58:09 +01:00
metamask-extension/shared/modules/fetch-with-timeout.js
Erik Marks 76a2a9bb8b
@metamask/eslint config@5.0.0 (#10358)
* @metamask/eslint-config@5.0.0
* Update eslintrc and prettierrc
* yarn lint:fix
2021-02-04 10:15:23 -08:00

30 lines
684 B
JavaScript

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;