Search code examples
node.jsreturn

How to return data from inside promise.all


findCustomerOrders(customerId) {
    return APIService.getCustomerOrders(this, customerId)
      .then((data) =>  {
        data.map(order => {
          return Promise.all([APIService.getShippingAddress(this, order.id), APIService.getProducts(this,order.id), APIService.getCustomerById(this, customerId)])
          .then((returnedData)=>{
            return buildOrder(returnedData);
          });
      });
   });
  }

The function where the data is to be returned is

findCustomerOrders(1)
.then((final) =>{console.log(final)});

I have to return the data which is returned by buildOrder Function, Due to Promise.all() I am not able to get the data back and it is showing the returned value as undefined. buildOrder function is correctly returning the value but the problem is with the above block, that too only with the return statements Please help me out.


Solution

  • I got the solution for this from my mentor :

    findCustomerOrders(customerId) {
        return APIService.getCustomerOrders(this, customerId)
          .then(orders => orders.map(order => Promise.all([APIService.getShippingAddress(this, order.id), APIService.getProducts(this, order.id), APIService.getCustomerById(this, customerId)])
            .then((returnedData) => {
              return buildOrder(order);
            })))
          .then(promises => Promise.all(promises));
      }