Created
October 30, 2019 07:05
-
-
Save joeytwiddle/3bda3f6c656c4a072a5e75f1ffb4b768 to your computer and use it in GitHub Desktop.
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
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms)); | |
const fn = (ms) => delay(ms).then(() => console.log('done after ' + ms)); | |
// Sequential | |
async function test1() { | |
const [one, two] = [await fn(2000), await fn(1000)]; | |
console.log('mikael1 finished'); | |
} | |
// Parallel | |
async function test2() { | |
const [one, two] = await Promise.all([fn(2000), fn(1000)]); | |
console.log('mikael2 finished'); | |
} | |
// Also parallel | |
async function test3() { | |
const [prom1, prom2] = [fn(2000), fn(1000)]; | |
const [one, two] = [await prom1, await prom2]; | |
console.log('joey1 finished'); | |
} | |
Promise.resolve().then(test1).then(test2).then(test3).catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment