delete_facebook
delete_facebook

Reputation: 47

How do I resolve nested promises in an object

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!

Upvotes: 0

Views: 311

Answers (1)

Cesare Polonara
Cesare Polonara

Reputation: 3860

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

Upvotes: 3

Related Questions