Created
August 13, 2021 17:22
-
-
Save santuman/fb52ebdf5f2161e2343466ee57b47cc2 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 a = item => new Promise(resolve => setTimeout(() => resolve(item), 10)) | |
const b = item => new Promise(resolve => setTimeout(() => resolve(item), 100)) | |
const c = item => new Promise(resolve => setTimeout(() => resolve(item), 200)) | |
// Parallel | |
const parallel = async () => { | |
const processes = [a(), b(), c()] | |
const [output1, output2, output3] = await Promise.all(processes) | |
return `parallel is done: ${output1} ${output2} ${output3}` | |
} | |
// Race | |
const race = async () => { | |
const processes = [a(), b(), c()] | |
const output = await Promise.race(processes) | |
return `Race is done: ${output}` | |
} | |
// Sequence | |
const race = async () => { | |
const output1 = await a() | |
const output2 = await b() | |
const output3 = await c() | |
return `Sequence is done: ${output1} ${output2} ${output3}` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment