Skip to content

Instantly share code, notes, and snippets.

@thinkjson
Created December 15, 2017 22:11
Show Gist options
  • Select an option

  • Save thinkjson/b1b716027457b5d7f491cf4fca361ad7 to your computer and use it in GitHub Desktop.

Select an option

Save thinkjson/b1b716027457b5d7f491cf4fca361ad7 to your computer and use it in GitHub Desktop.
Promise.race example
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!`);
});
Alice won the race!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment