Created
September 13, 2018 08:35
-
-
Save fxp/797d070ab10184cdde03608ddf539f8d to your computer and use it in GitHub Desktop.
Timeout promise version and usage
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 promiseTimeout(time) { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => reject('timeout'), time); | |
}); | |
}; | |
let promiseB = new Promise((resolve, reject) => { | |
setTimeout(() => { | |
reject('Promise win!'); | |
}, 400) | |
}) | |
let race = Promise.race([ | |
promiseTimeout(200), | |
promiseB | |
]) | |
race.then((res) => console.log(res), (err) => console.error(err)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment