Last active
March 28, 2019 09:56
-
-
Save simon-contreras-deel/632ad36096eb480f1a8441b691ea30a9 to your computer and use it in GitHub Desktop.
[async / await parallel] #js
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
function timeout(ms) { | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
} | |
async function a() { | |
await timeout(3000); | |
console.log('a') | |
return 'a' | |
} | |
async function b() { | |
await timeout(2000); | |
console.log('b') | |
return 'b' | |
} | |
// serie | |
// async function run() { | |
// await a() | |
// await b() | |
// } | |
// parallel 1 | |
// async function run() { | |
// await Promise.all([a(), b()]) | |
// } | |
// parallel 2 | |
async function run() { | |
let fns = [a, b] | |
await Promise.all(fns.map(fn => fn())).then(results => console.log('results', results)) | |
} | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment