Skip to content
Advertisement

Append to array in async for loop

I’ve got this function which contains other nested async functions.

I’m unzipping a zipfile and then appending each HTMLImageElement to an array.

However, the array is printing like this

console print

16 is the correct number of images I’m expecting, but they’re undefined when I console.log() them.

JavaScript

I’m mapping the promise of each image to an array then doing Promise.all() on this array, so I’m not sure why it’s still coming back as undefined.

Inside of mediaFiles.map() I do some prints and they print the img data successfully.

Image prints

How can I fill this array with the HTMLImageElements?

Advertisement

Answer

You don’t return your promise in the map function:

JavaScript

Must become :

JavaScript

For your second question, you must await your promises and return their results :

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