Created
September 21, 2019 13:46
-
-
Save kamataryo/64a99bc7f368698ea50e314e6079b814 to your computer and use it in GitHub Desktop.
Delay your promise
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
const wait = (msec: number) => | |
new Promise(resolve => setTimeout(resolve, msec)); | |
const catched = Symbol("is-catched"); | |
const delayPromise = (promise: Promise<any>, msec: number) => { | |
const catchedPromise = promise.catch(err => ({ [catched]: err })); | |
return Promise.all([catchedPromise, wait(msec)]).then(result => { | |
if (result[0][catched]) { | |
throw result[0][catched]; | |
} else { | |
return result[0]; | |
} | |
}); | |
}; | |
export default delayPromise; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment