1
00:00:00,000 --> 00:00:04,040
Why does my JavaScript say upload complete

2
00:00:04,040 --> 00:00:07,120
when all three files are still uploading?

3
00:00:07,120 --> 00:00:09,280
I used await.

4
00:00:09,460 --> 00:00:12,770
Each async call returns a promise:

5
00:00:12,770 --> 00:00:15,080
an object for a future result.

6
00:00:15,080 --> 00:00:19,360
But forEach ignores those promises and returns undefined.

7
00:00:19,360 --> 00:00:22,900
Your outer await gets undefined, not the uploads.

8
00:00:23,080 --> 00:00:26,400
So the uploads keep going, but my

9
00:00:26,400 --> 00:00:29,720
success message does not wait for them?

10
00:00:29,900 --> 00:00:31,320
Right.

11
00:00:31,320 --> 00:00:35,390
To upload one file at a time, use for of.

12
00:00:35,390 --> 00:00:39,800
Await pauses this async function until each upload finishes.

13
00:00:39,800 --> 00:00:42,100
Only then does the next one start.

14
00:00:42,280 --> 00:00:44,480
For these three independent

15
00:00:44,480 --> 00:00:47,200
files, map collects their promises.

16
00:00:47,200 --> 00:00:50,400
Promise all waits for every upload to succeed.

17
00:00:50,400 --> 00:00:51,720
Then we show the message.

18
00:00:51,900 --> 00:00:55,400
Catch errors before showing success.

19
00:00:55,400 --> 00:00:59,720
Promise all does not cancel other uploads if one fails.

20
00:00:59,720 --> 00:01:02,020
For a large list, limit how

21
00:01:02,020 --> 00:01:03,260
many uploads run at once.

22
00:01:03,440 --> 00:01:06,460
Nothing uploaded.

23
00:01:06,460 --> 00:01:09,040
Everything reported.

24
00:01:09,040 --> 00:01:11,200
My code is ready to become a manager.
