Skip to content

Instantly share code, notes, and snippets.

@seveibar
Created January 10, 2017 01:48
Show Gist options
  • Save seveibar/f04cb1a1046a45937b227498cf91636f to your computer and use it in GitHub Desktop.
Save seveibar/f04cb1a1046a45937b227498cf91636f to your computer and use it in GitHub Desktop.
async function add(x:number,y:number):Promise<number>{
return x + y
}
const a = [1,2,3,4,5]
function PARALLEL(){
return Promise.all(a.map((v) => add(v,1)))
}
function SERIAL(){
return a
.map((x) => () => add(x, 1))
.reduce((acc, pf) =>
acc.then( ar =>
pf().then( v => ar.concat([v]) )
)
, Promise.resolve(Promise.resolve([])))
}
SERIAL().then((answer)=>console.log(`SERIAL ${answer.toString()}`))
PARALLEL().then((answer)=>console.log(`PARALLEL ${answer.toString()}`))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment