Skip to content

Instantly share code, notes, and snippets.

@serkodev
Last active March 29, 2022 12:42
Show Gist options
  • Select an option

  • Save serkodev/b446ee9d100073777165c5bf7424ff2a to your computer and use it in GitHub Desktop.

Select an option

Save serkodev/b446ee9d100073777165c5bf7424ff2a to your computer and use it in GitHub Desktop.
Typescript fetch with timeout
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