Skip to content

Instantly share code, notes, and snippets.

@joeytwiddle
Created October 30, 2019 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeytwiddle/3bda3f6c656c4a072a5e75f1ffb4b768 to your computer and use it in GitHub Desktop.
Save joeytwiddle/3bda3f6c656c4a072a5e75f1ffb4b768 to your computer and use it in GitHub Desktop.
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