Skip to content
Advertisement

How to populate array with values from API using “fetch” in javascript function

I’m trying to write a function that builds an array where each value of the array is a value pulled from an API using a unique url, once the value is fetched from the API it is pushed into the existing array.

Basically when the function is called I want to populate an array with fetched API data for each stock ticker listed in the List, what i have so far doesnt seem to be working though

JavaScript

Advertisement

Answer

You’ll need to await for all of the fetches to finish before you log things.

Reworking your code to async/await functions and adding the required Promise.all():

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement