Last active
April 5, 2022 18:18
-
-
Save SanariSan/07d3b53e947d49b97df13e57959e3edf to your computer and use it in GitHub Desktop.
Fetch timeout using abort controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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