In node.js I am trying to get a list of bid and ask prices from a trade exchange website (within an async function). Within the foreach statement I can console.info() the object data(on each iteration) but when I put all of this into an array and then return it to another function it passes as 'undefined'.
const symbolPrice = async() => {
let symbolObj = {}
let symbolList = []
await bookTickers((error, ticker) => {
ticker.forEach(symbol => {
if (symbol.symbol.toUpperCase().startsWith(starts.toUpperCase())) {
symbolObj = {
symbol: symbol.symbol,
bid: symbol.bidPrice,
ask: symbol.askPrice
}
console.info(symbolObj);
}
symbolList.push(symbolObj)
});
const results = Promise.all(symbolList)
return results;
});
}
const symbolPriceTest = async() => {
const res = await symbolPrice(null, 'ETH', true);
console.log(res)
}
I have tried pretty much everything I can find on the internet like different awaits/Promise.all()'s. I do admit I am not as familiar with async coding as I would like to be.
bookTickers()works or what you're trying to do with the result you generate so we can't really offer a complete solution. For starters, declare all your variables in the proper scope. Don't reuse variable names in a loop. Only usePromise.all()with an array of promises. Only useawaitwhen you are awaiting a promise. Don't mix promises and plain callbacks.bookTickers()retrieves a current symbol bid and ask price through a WebSocket. Withtickerbeing an array of objects(of the symbols and their bid/ask price).