Skip to content

Instantly share code, notes, and snippets.

@SanariSan
Last active April 5, 2022 18:18
Show Gist options
  • Save SanariSan/07d3b53e947d49b97df13e57959e3edf to your computer and use it in GitHub Desktop.
Save SanariSan/07d3b53e947d49b97df13e57959e3edf to your computer and use it in GitHub Desktop.
Fetch timeout using abort controller
// by https://stackoverflow.com/a/50101022/15516769
// more info https://developers.google.com/web/updates/2017/09/abortable-fetch
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 30 * 1000);
fetch(url, { signal: controller.signal })
.then((response) => {
// completed request before timeout fired
// If you only wanted to timeout the request, not the response, add:
clearTimeout(timeoutId)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment