Last active
January 26, 2019 13:27
-
-
Save cwbak/6f33ebfe19b9600dfe5070154ea4833f 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
function asyncFunc(e) { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => resolve(e), e * 1000); | |
}); | |
} | |
const arr = [1, 2, 3]; | |
let final = []; | |
function workMyCollection(arr) { | |
return arr.reduce((promise, item) => { | |
return promise.then((result) => { | |
console.log(`item ${item}`); | |
return asyncFunc(item).then(result => final.push(result)); | |
}) | |
.catch(console.error); | |
}, Promise.resolve()); | |
} | |
workMyCollection(arr) | |
.then(() => console.log(`FINAL RESULT is ${final}`)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment