0

Code:

async function main() {
  const arr = [];
  symbols.forEach((symbol) => {
    const record = {
      [symbol]: {
        quote: priceQuote(token, symbol),
        history: makePriceHistoryRequest(token, symbol, slow * 2),
      }
    }
    arr.push(record);
  })
    return arr;
}

I call main as follows:

main().then(res=>{
  console.log(res);
})

It returns quote and history as Promise. How can I get into nested pending promise?

Thanks!

  • Didn't get your point. If you are calling main then which promise is pending according to your code? – Amit Gandole Apr 08 '22 at 19:31
  • 3
    Start by [not using `forEach`](https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop). Instead, create an array of promises, then `Promise.all` them – Bergi Apr 08 '22 at 19:37

1 Answers1

2

If quote and history are promises it means that priceQuote and makePriceHistoryRequest are functions that return promises, in this case, you need to make the callback that iterates them async, not the wrapper:

 function main() {
   return Promise.all(symbols.map(async (symbol) => {
     const record = {
      [symbol]: {
        quote: await priceQuote(token, symbol),
        history: await makePriceHistoryRequest(token, symbol, slow * 2),
      }
    }
    return record
  })  
}

main().then(value => console.log(value) ) 
Cesare Polonara
  • 3,473
  • 1
  • 9
  • 19