Created
January 15, 2018 05:25
-
-
Save schahriar/e007901b0238b93db83f6b4f2984f58f to your computer and use it in GitHub Desktop.
Limits race #async-await #medium
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 randAsyncTimer = (i) => { | |
// Timeout within 1 second | |
const timeout = Math.floor(Math.random() * 1000); | |
return new Promise((resolve) => setTimeout(() => resolve(i), timeout)); | |
}; | |
async () => { | |
let calls = [randAsyncTimer(1), randAsyncTimer(2), randAsyncTimer(3)]; | |
// Start the race | |
const result = await Promise.race(calls); | |
console.log(result); | |
// Possible outputs are between 1 to 3 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment