Skip to content

Instantly share code, notes, and snippets.

@schahriar
Created January 15, 2018 05:25
Show Gist options
  • Save schahriar/e007901b0238b93db83f6b4f2984f58f to your computer and use it in GitHub Desktop.
Save schahriar/e007901b0238b93db83f6b4f2984f58f to your computer and use it in GitHub Desktop.
Limits race #async-await #medium
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