Skip to content

Instantly share code, notes, and snippets.

@gskachkov
Last active June 3, 2017 10:20
Show Gist options
  • Save gskachkov/1409d93d252249b619126c02511e4932 to your computer and use it in GitHub Desktop.
Save gskachkov/1409d93d252249b619126c02511e4932 to your computer and use it in GitHub Desktop.
Example of using promise all and race function with async function
//TODO: Add console.log results
async function foo () {
console.log('foo-start');
const result = await 'a';
console.log('foo-end');
return result;
}
async function boo() {
console.log('boo-start');
const result = await 'b';
console.log('boo-end');
return result;
}
const all_promise = Promise.all([foo(), boo()]);
all_promise.then(results => {
console.log('all `foo` result:', results[0]);
console.log('all `boo` result:', results[1]);
});
// foo-start
// boo-start
// foo-end
// boo-end
// all `foo` result: a
// all `boo` result: b
const race_promise = Promise.race([foo(), boo()]);
race_promise.then(result => {
console.log('race result:', result);
});
// foo-start
// boo-start
// foo-end
// boo-end
// race result: a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment