Last active
June 20, 2019 13:18
-
-
Save marr/4c4b0e9773f8a7c2a726bca7db06c590 to your computer and use it in GitHub Desktop.
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
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) ); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
credit: https://davidwalsh.name/fetch-timeout#comment-511297