Uploading multiple images to Firebase using Array

Check out this post for some terminology.

I would suggest breaking your code down into very small building blocks, and declaring proper types for all function parameters and return values of each building block. Decide which of those three types (in the linked post) of functions each needs to be, and type accordingly.

Do not touch this inside these building blocks. Make each static if that helps. Every block has to be totally self-contained, taking everything it needs as a parameter and returning whatever its caller will rely on.

This exercise helps me untangle complex operations and understand what improper assumptions I am making about state management and asynchronous event timing. I think it will help you in this situation as well.