Last active
June 3, 2017 10:20
-
-
Save gskachkov/1409d93d252249b619126c02511e4932 to your computer and use it in GitHub Desktop.
Example of using promise all and race function with async function
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
//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