Created
December 15, 2017 22:11
-
-
Save thinkjson/b1b716027457b5d7f491cf4fca361ad7 to your computer and use it in GitHub Desktop.
Promise.race example
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
| function race(player: string) { | |
| return new Promise((resolve, reject) => { | |
| setTimeout(() => { | |
| resolve(player); | |
| }, Math.random() * 20) | |
| }) | |
| } | |
| Promise.race([ | |
| race('Bob'), | |
| race('Alice'), | |
| race('Fred'), | |
| race('George') | |
| ]).then(player => { | |
| console.log(`${player} won the race!`); | |
| }); |
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
| Alice won the race! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment