Skip to content

Instantly share code, notes, and snippets.

@oleurud
Last active March 28, 2019 09:56
Show Gist options
  • Save oleurud/632ad36096eb480f1a8441b691ea30a9 to your computer and use it in GitHub Desktop.
Save oleurud/632ad36096eb480f1a8441b691ea30a9 to your computer and use it in GitHub Desktop.
[async / await parallel] #js
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