Created
January 10, 2017 01:48
-
-
Save seveibar/f04cb1a1046a45937b227498cf91636f 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
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