Skip to content

Instantly share code, notes, and snippets.

@santuman
Created August 13, 2021 17:22
Show Gist options
  • Save santuman/fb52ebdf5f2161e2343466ee57b47cc2 to your computer and use it in GitHub Desktop.
Save santuman/fb52ebdf5f2161e2343466ee57b47cc2 to your computer and use it in GitHub Desktop.
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