0

I'm working with an AWS lambda function that has a sort of 'map/reduce' feel to it. But the 'map' part of it, that is the part that does multiple calls is async.

Using the Node 6 STD lib, is there a dynamic way to get all results returned to a shared spot.

What I've thought about so far:

  • await async is a great abstraction but isn't in node 6 to my knowledge, just node 8.
  • array.reduce says it takes a callback but the structure of an http request seems not to qualify though I certainly may be wrong
  • I've thought about a really suboptimal solution, where each callback puts into a shared queue or array. And I have a loop after all the requests that checks the length of the array - I don't like this solution though

Could you guys point me in the right direction or show me some code that would do this?

Jono
  • 3,393
  • 6
  • 33
  • 48
  • Convert all asynchronous functions to the functions that return promise, and use `Promise.all` to get all results. – alexmac Oct 02 '17 at 15:00
  • @alexmac, thank will do! Any direction on what STD lib does http with promises, or a lib that converts callbacks to promises? – Jono Oct 02 '17 at 15:13
  • Node.js natively supports Promises, but there are some powerful promise libraries, such as Bluebird or Q. Bluebird has `promifiy` and `promisifyAll` methods to convert a single function or all functions to the promises. – alexmac Oct 02 '17 at 15:17
  • Sounds good Alex, I'll post the final code here when I get it :-) – Jono Oct 02 '17 at 15:25

1 Answers1

0

Bluebird is your friend.

To convert callback functions into promises you can use .promisify() or .fromCallback().

To do map/reduce over an array of promises, you can use .map() and .reduce().

Noel Llevares
  • 15,018
  • 3
  • 57
  • 81