1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-27 21:00:13 +01:00
metamask-extension/app/scripts/lib/fetch-with-timeout.js

24 lines
542 B
JavaScript
Raw Normal View History

const fetchWithTimeout = ({ timeout = 120000 } = {}) => {
2020-11-03 00:41:28 +01:00
return async function _fetch(url, opts) {
const abortController = new window.AbortController()
const abortSignal = abortController.signal
const f = window.fetch(url, {
...opts,
signal: abortSignal,
})
const timer = setTimeout(() => abortController.abort(), timeout)
try {
const res = await f
clearTimeout(timer)
return res
} catch (e) {
clearTimeout(timer)
throw e
}
}
}
export default fetchWithTimeout