I think this is a fairly simple question, but I primarily program in ruby and ma having trouble wrapping my head around asynchronous functions in javascript. Specifically, I have a method that is supposed to populate an array with results fetched asynchronously from an API. I am able to get the results fine, but I can't seem to return the array after it has been populated. Rather, the return statement executes before the promise is resolved, so an empty array is returned. Simplified code example below:
async function getAnimalSounds(animals){
var sounds = []
for (const a of animals){
asynchronously_fetch_sound(a).then((result) => {
sounds.push(result)
})
}
return sounds // sounds returns as an empty array
}
Thank you in advance!