0

I have a function that calls an exported async/await function I need to push the value returned in an array and return it but I get an empty array I know I have to use a promise but can't figure out how to do it correctly.

function calculatePrice() {
  let nearestPickUps = findNearestPickups();
  let prices = [];
  nearestPickUps.forEach(loc => {
    let pickup = loc.nearest.pickup;
    calcPrice
      .getPriceList()
      .then(function(result) {
        prices.push(result); // push to array
        console.log(result); // value is retured
      });
  });
  return prices; // returns empty array
}
Harry
  • 1,021
  • 4
  • 21
  • 41
  • What does `getPriceList()` look like? – Mark C. Oct 28 '19 at 13:58
  • because `foreach` not waiting for resolving or rejecting promise – Saeed Jalali Oct 28 '19 at 14:00
  • You're doing asynchronous operations in the function but you're returning synchronously (without blocking execution). This doesn't make sense. See the question yours is a duplicate of to understand better how to fix the situation. – Xiyng Oct 28 '19 at 14:01

0 Answers0