Created
March 28, 2018 22:32
-
-
Save garystorey/95b6b5394d034c48aedd5150892217bf to your computer and use it in GitHub Desktop.
promise based 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
/* https://github.com/github/fetch/issues/175 */ | |
function timeout(ms, promise) { | |
return new Promise(function(resolve, reject) { | |
setTimeout(function() { | |
reject(new Error("timeout")) | |
}, ms) | |
promise.then(resolve, reject) | |
}) | |
} | |
/*Usage*/ | |
timeout(1000, fetch('/hello')).then(function(response) { | |
// process response | |
}).catch(function(error) { | |
// might be a timeout error | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment