Bnaya

You probobly don't need a helper library for Promise/Async a

Mar 12th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // You probobly don't need a helper library for Promise/Async after all
  2. const seedValues = [Promise.resolve(777), Promise.resolve(888)];
  3.  
  4. // reduce
  5. // Promise based
  6. seedValues
  7.   .reduce((accumulator, currentValue) =>
  8.     accumulator.then(v => currentValue.then(cv => v + cv))
  9.   )
  10.   .then(console.log.bind(console));
  11.  
  12. // async
  13. seedValues
  14.   .reduce(async (acc, c) => (await acc) + (await c))
  15.   .then(console.log.bind(console));
  16.  
  17. // simple map
  18. // Promise based
  19. Promise.all(seedValues.map(p => p.then(v => v.toString()))).then(
  20.   console.log.bind(console)
  21. );
  22.  
  23. // async
  24. Promise.all(seedValues.map(async p => (await p).toString())).then(
  25.   console.log.bind(console)
  26. );
Advertisement
Add Comment
Please, Sign In to add comment