1
00:00:01,410 --> 00:00:03,505
Sometimes when you queue up promises, you

2
00:00:03,505 --> 00:00:06,468
want as much data as possible to come

3
00:00:06,468 --> 00:00:09,212
back. If one or maybe even two promises

4
00:00:09,212 --> 00:00:11,678
fail, that's okay, so long as you get the

5
00:00:11,678 --> 00:00:15,818
rest of the data. To see how to do this,

6
00:00:15,818 --> 00:00:17,937
open up that creating.mjs file, and modify

7
00:00:17,937 --> 00:00:21,286
the code for the allSettled function.

8
00:00:21,286 --> 00:00:23,109
Start by copying the code from the

9
00:00:23,109 --> 00:00:28,181
previous clip, the all function, into this

10
00:00:28,181 --> 00:00:30,730
function. The first thing to change is the

11
00:00:30,730 --> 00:00:35,570
name, instead of all, it's allSettled;

12
00:00:35,570 --> 00:00:37,283
allSettled is similar to all, but it's

13
00:00:37,283 --> 00:00:39,304
different enough that I want to pop out to

14
00:00:39,304 --> 00:00:41,997
some slides to explain it before we use

15
00:00:41,997 --> 00:00:46,456
it. First, the data that's passed back is

16
00:00:46,456 --> 00:00:49,386
different. All returns the results object

17
00:00:49,386 --> 00:00:52,829
as part of an array, but allSettled

18
00:00:52,829 --> 00:00:56,514
returns a different shape. It has two

19
00:00:56,514 --> 00:00:59,129
keys. The status key will be either

20
00:00:59,129 --> 00:01:02,278
fulfilled or rejected, and then the second

21
00:01:02,278 --> 00:01:06,538
key will be either value, if the status is

22
00:01:06,538 --> 00:01:09,588
fulfilled; or reason, if the status is

23
00:01:09,588 --> 00:01:12,269
rejected. That is, the then function on

24
00:01:12,269 --> 00:01:14,491
allSettled will return all promises even

25
00:01:14,491 --> 00:01:18,552
if they were rejected, which leads to a

26
00:01:18,552 --> 00:01:21,096
second difference. We don't need a catch

27
00:01:21,096 --> 00:01:22,777
block, because this promise will resolve

28
00:01:22,777 --> 00:01:26,564
with an array of data, including rejected

29
00:01:26,564 --> 00:01:28,774
promises. Even though a catch block is not

30
00:01:28,774 --> 00:01:31,111
specifically needed, it's still a good

31
00:01:31,111 --> 00:01:33,918
practice to include. It will help catch

32
00:01:33,918 --> 00:01:37,076
any errors that might occur inside of your

33
00:01:37,076 --> 00:01:39,995
then block, for example. With that very

34
00:01:39,995 --> 00:01:41,690
brief explanation, let's hop back to the

35
00:01:41,690 --> 00:01:44,108
code and take a look at what this would

36
00:01:44,108 --> 00:01:46,438
look like. Start by changing what's

37
00:01:46,438 --> 00:01:49,906
received in the then function. Instead of

38
00:01:49,906 --> 00:01:54,442
individual values, simply pass in a values

39
00:01:54,442 --> 00:01:56,661
parameter. Next, let's add some code to

40
00:01:56,661 --> 00:01:59,368
show what the status is, and a glimpse of

41
00:01:59,368 --> 00:02:02,524
the data we get back, and to do that, we

42
00:02:02,524 --> 00:02:07,075
can delete all of the body of the then

43
00:02:07,075 --> 00:02:09,747
function. This code first of all checks

44
00:02:09,747 --> 00:02:12,536
the status, and it checks to see if it's

45
00:02:12,536 --> 00:02:14,898
fulfilled. If it is fulfilled, then it

46
00:02:14,898 --> 00:02:16,904
constructs a string that includes the data

47
00:02:16,904 --> 00:02:19,443
that was passed back, and in this case,

48
00:02:19,443 --> 00:02:23,296
it's only taking the first element of that

49
00:02:23,296 --> 00:02:25,596
array. If it was not fulfilled, it'll set

50
00:02:25,596 --> 00:02:27,940
the status to REJECTED, and give the

51
00:02:27,940 --> 00:02:30,781
reason why; and notice with both of these

52
00:02:30,781 --> 00:02:32,666
strings, there's an empty space at the

53
00:02:32,666 --> 00:02:34,594
end, and that'll just help for formatting

54
00:02:34,594 --> 00:02:38,536
on a smaller screen. Reload the website in

55
00:02:38,536 --> 00:02:42,378
your browser, and click the All Settled

56
00:02:42,378 --> 00:02:44,597
button. The results on the window appear

57
00:02:44,597 --> 00:02:47,140
after one and a half seconds, even though

58
00:02:47,140 --> 00:02:50,343
the addressTypes call rejected nearly

59
00:02:50,343 --> 00:02:52,901
instantly. The data shows the first three

60
00:02:52,901 --> 00:02:55,740
promises were all fulfilled, and the last

61
00:02:55,740 --> 00:03:00,013
one was rejected with a 404. By changing

62
00:03:00,013 --> 00:03:02,646
the name of the function from all to

63
00:03:02,646 --> 00:03:05,240
allSettled, and by expecting a different

64
00:03:05,240 --> 00:03:07,847
object shape in the then function, you're

65
00:03:07,847 --> 00:03:10,200
able to queue up several promises that

66
00:03:10,200 --> 00:03:12,653
wait until they're all settled before

67
00:03:12,653 --> 00:03:15,618
calling the then function. One thing to

68
00:03:15,618 --> 00:03:17,542
note here, as of the time of this

69
00:03:17,542 --> 00:03:20,111
recording, not all browsers support

70
00:03:20,111 --> 00:03:22,839
allSettled, the ones that do are primarily

71
00:03:22,839 --> 00:03:26,052
Chrome and Firefox. To check the status,

72
00:03:26,052 --> 00:03:29,899
check out the MDN page listed on the

73
00:03:29,899 --> 00:03:31,485
screen. In the next clip, I'll show you

74
00:03:31,485 --> 00:03:33,923
how you can use promises to get data from

75
00:03:33,923 --> 00:03:39,000
the fastest API endpoint, and ignore all the other data.

