Skip to content

Instantly share code, notes, and snippets.

@marr
Last active June 20, 2019 13:18
Show Gist options
  • Save marr/4c4b0e9773f8a7c2a726bca7db06c590 to your computer and use it in GitHub Desktop.
Save marr/4c4b0e9773f8a7c2a726bca7db06c590 to your computer and use it in GitHub Desktop.
function fetchWithTimeout( url, timeout ) {
return new Promise( (resolve, reject) => {
// Set timeout timer
let timer = setTimeout(
() => reject( new Error('Request timed out') ),
timeout
);
fetch( url ).then(
response => resolve( response ),
err => reject( err )
).finally( () => clearTimeout(timer) );
})
}
@marr
Copy link
Author

marr commented Jun 20, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment