Index
Map With Async Function Returns Promises


map
  • map with async function
  • Result is promises
  • Resolve promises using Promise.all
  • Result is proper objects

SUGGESTION -- Use js traditional code style with promises (async and await). Keep it simple. Do NOT use functional programming style -- there are gotchas (with fp) as shown here.
let a, b;
a = [10, 20, 30, 40];
1
b = a.map(async n => await n / 10); console.log(b); //Map gives us promises:
2
//[ Promise { 1 }, Promise { 2 }, Promise { 3 }, Promise { 4 } ] //Resolve promises
3
b = await Promise.all(b); console.log(b); //Now we have proper objects
4
//[ 1, 2, 3, 4 ]


Reduce With Async Function Returns Promise


reduce
  • reduce with async function
  • Result is a promise
  • Resolve promises using Promise.all
  • Result is objects but still a bit of a promise -- see warnings above and below

SUGGESTION -- Use js traditional code style with promises (async and await). Keep it simple. Do NOT use functional programming style -- there are gotchas (with fp) as shown here.
let a, promise;
a = [10, 20, 30, 40];
1
promise = a.reduce(async (accum, each) => await (accum + each)); console.log(promise); //Map gives us promises:
2
//Promise { <pending> } let resolvedValue;
3
resolvedValue = await promise; console.log(resolvedValue); //Map gives us promises:
4
//[object Promise]40