Last active
July 14, 2017 19:07
-
-
Save gt3/522b31d7ab6f138c9cf8f2b11c1a5622 to your computer and use it in GitHub Desktop.
This file contains 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
let arr = [1, 2, 3], res = []; | |
function later(i) { | |
console.log('sched:', i) | |
return new Promise((res, rej) => { | |
setTimeout(() => { | |
console.log('resolve:', i) | |
res(i) | |
}, 1000*i) | |
}) | |
} | |
function all() { | |
return Promise.all(arr.reduce((acc, curr) => { | |
acc.push(later(curr).then(i => res.push(i))) | |
return acc | |
}, [])) | |
} | |
//all() | |
function each() { | |
return arr.reduce((acc, curr) => { | |
return acc.then(i => later(curr).then(i => res.push(i))) | |
}, Promise.resolve()) | |
} | |
//each() | |
async function asyncEach() { | |
for(let curr of arr) { | |
let i = await later(curr) | |
res.push(i) | |
} | |
} | |
//asyncEach() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment