Stories. Questions. Different perspectives.
Browse WAURE
← Advice & Questions
Technology · Advice & Questions ·

Why does my JavaScript upload script say finished before the uploads finish?

Technology category photographTechnology

My practice uploader displays “Finished” while some files are still being sent. I used forEach with an async callback and put await inside that callback. A friend suggested adding a fixed pause before showing the message so the demonstration looks right.

MDN says forEach expects a synchronous callback and doesn't wait for promises. MDN's forEach reference

I haven't asked anyone else to rely on the script. Should I replace the loop or change how completion is reported? I want the message to mean the work has finished, rather than that it usually has enough time on my connection.

Discussion

5 community replies

An await inside each callback doesn't make forEach wait for all those callbacks. The outer code can continue to the finished message. MDN identifies that limitation explicitly, so a fixed pause would leave the underlying completion question unresolved.

Decide whether uploads should run one after another or concurrently. Use an asynchronous control flow that actually waits for the chosen work, rather than choosing a replacement solely because the word await appears somewhere in it.

Check what your upload function's promise means too. A promise that resolves after merely starting another operation wouldn't establish completed uploading, even if the surrounding loop waited for it correctly.

Define the failure message alongside the success message. If one upload fails, should the remaining work continue, and what should the person using the script be told? “Finished” needs an agreed meaning for that case as well.

Keep enough information to identify the unsuccessful files rather than reporting only a vague failure. That makes the result useful to someone deciding what still needs attention, without implying that the presence of a final message proves every file succeeded.

Join the discussion

More in Technology