Created
September 26, 2016 10:28
-
-
Save yezhiming/8d08f0fe5faf15d9c449c7f05d0f7ef2 to your computer and use it in GitHub Desktop.
timeout promise use with window.fetch
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
// ref: https://github.com/github/fetch/issues/175 | |
function timeoutPromise(ms, promise) { | |
return new Promise( (resolve, reject) => { | |
const timeoutId = setTimeout( () => { | |
reject( new Error("timeout") ) | |
}); | |
promise.then( | |
(res) => { | |
clearTimeout(timeoutId); | |
resolve(res); | |
}, | |
(err) => { | |
clearTimeout(timeoutId); | |
reject(err); | |
} | |
) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment