Created
January 30, 2020 19:55
-
-
Save munkacsitomi/362a613bb1c8bcecb02ab9f23c04ecf5 to your computer and use it in GitHub Desktop.
Sequential and Parallel execution with async/await
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 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