Coalescing (combining) callbacks in node.js 6

236 Views Asked by At

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?

1

There are 1 best solutions below

0
Noel Llevares On

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().