Created
July 9, 2019 21:24
-
-
Save jmakeig/937385e8ae3e3eb8a4e11dc49d47d718 to your computer and use it in GitHub Desktop.
Promise 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#issuecomment-216791333 | |
function timeoutPromise(ms, promise) { | |
return new Promise((resolve, reject) => { | |
const timeoutId = setTimeout(() => { | |
reject(new Error('promise timeout')); | |
}, ms); | |
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