Last active
March 29, 2022 12:42
-
-
Save serkodev/b446ee9d100073777165c5bf7424ff2a to your computer and use it in GitHub Desktop.
Typescript fetch with timeout
This file contains hidden or 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
| function fetchTimeout(url: string, init?: RequestInit, timeout = 5000): Promise<unknown> { | |
| return Promise.race([ | |
| fetch(url, init), | |
| new Promise((_, reject) => setTimeout(() => reject(), timeout)) | |
| ]); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment