Advertisement
Guest User

Untitled

a guest
May 26th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. function asyncFunc(e) {
  2. return new Promise((resolve, reject) => {
  3. setTimeout(() => resolve(e), e * 1000);
  4. });
  5. }
  6.  
  7. const arr = [1, 2, 3];
  8. let final = [];
  9.  
  10. function workMyCollection(arr) {
  11. return arr.reduce((promise, item) => {
  12. return promise
  13. .then((result) => {
  14. console.log(`item ${item}`);
  15. return asyncFunc(item).then(result => final.push(result));
  16. })
  17. .catch(console.error);
  18. }, Promise.resolve());
  19. }
  20.  
  21. workMyCollection(arr)
  22. .then(() => console.log(`FINAL RESULT is ${final}`));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement