1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/app/scripts/lib/fetch-with-timeout.js
2020-12-23 10:15:07 -06:00

24 lines
521 B
JavaScript

const fetchWithTimeout = ({ timeout = 120000 } = {}) => {
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 fetchWithTimeout