Skip to content

Instantly share code, notes, and snippets.

@munkacsitomi
Created January 30, 2020 19:55
Show Gist options
  • Save munkacsitomi/362a613bb1c8bcecb02ab9f23c04ecf5 to your computer and use it in GitHub Desktop.
Save munkacsitomi/362a613bb1c8bcecb02ab9f23c04ecf5 to your computer and use it in GitHub Desktop.
Sequential and Parallel execution with async/await
const pause = (data, time) => new Promise(resolve => setTimeout(() => resolve(data), time));
const sequentialParallel = async () => {
console.time('time');
// Sequential
// const first = await pause('first', 3000);
// const second = await pause('second', 2000);
// Parallel
// const firstPromise = pause('first', 3000);
// const secondPromise = pause('second', 2000);
// const first = await firstPromise;
// const second = await secondPromise;
// Also Parallel but much nicer, wow
const [first, second] = await Promise.all([pause('first', 3000), pause('second', 2000)]);
console.log('result:', first, second);
console.timeEnd('time');
};
sequentialParallel();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment