Skip to content

Instantly share code, notes, and snippets.

@cms
Last active June 4, 2019 16:45
Show Gist options
  • Save cms/4d1a101645cea74f199f1896c2ccc727 to your computer and use it in GitHub Desktop.
Save cms/4d1a101645cea74f199f1896c2ccc727 to your computer and use it in GitHub Desktop.
fetch with cancellation at timeout using AbortController
export default function fetchWithTimeout(url, options, timeout = 3000) {
const controller = new AbortController()
const { signal } = controller
setTimeout(() => controller.abort(), timeout)
return fetch(url, { ...options, signal })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment