DEV Community

Discussion on: Then After forEach

Collapse
 
matchojecky profile image
Mateusz • Edited
const orderPromises = orders.map(order => api.sendOrder(order));
Promise.all(orderPromisses).then(arrayOfResponses => {
    // do your stuff
})

You have over overengineered this and your solution wasn't really working as it should.

Collapse
 
forstermatth profile image
Matt Forster • Edited

Yes, This is definitely the solution! Node has native support for dealing with multiple async promises.

You could even remove the variable assignment (though JS is going to assign it to a temp anyways);


return Promise.all(orders.map(api.sendOrder))
  .then(allOrders => {
    ...
  });

Thats if sendOrder does not need its functional context. Semantically the same solution, though.