﻿1
00:00:01,270 --> 00:00:03,170
‫And now to finish the section,

2
00:00:03,170 --> 00:00:05,760
‫let me show you a nice trick on how we can

3
00:00:05,760 --> 00:00:10,040
‫basically run many promises all at the same time.

4
00:00:10,040 --> 00:00:12,690
‫So lets suppose that we actually wanted to get three

5
00:00:12,690 --> 00:00:15,200
‫random dog images and not just one.

6
00:00:15,200 --> 00:00:17,053
‫We could do something like this.

7
00:00:19,300 --> 00:00:23,740
‫So simply awaiting D3API calls, one after the other.

8
00:00:23,740 --> 00:00:26,600
‫But, why would we actually make the second API

9
00:00:26,600 --> 00:00:28,340
‫call wait for the first one,

10
00:00:28,340 --> 00:00:31,690
‫and the third one wait for the second one?

11
00:00:31,690 --> 00:00:34,320
‫That would just add unnecessary waiting time

12
00:00:34,320 --> 00:00:36,060
‫when we could just run all of these

13
00:00:36,060 --> 00:00:38,100
‫promises at the same time.

14
00:00:38,100 --> 00:00:40,920
‫So let me show you how we could do that.

15
00:00:40,920 --> 00:00:43,980
‫So, the solution is not going to be something like this,

16
00:00:43,980 --> 00:00:47,240
‫but instead, what we will do is to not await a

17
00:00:47,240 --> 00:00:49,990
‫promise at this point, but instead save

18
00:00:49,990 --> 00:00:52,143
‫the promise into a variable.

19
00:00:53,330 --> 00:00:56,100
‫So again, we want to now save the promise

20
00:00:56,100 --> 00:00:59,470
‫and not the resolved value of the promise.

21
00:00:59,470 --> 00:01:02,470
‫So, lets call this one here, result one, pro,

22
00:01:02,470 --> 00:01:03,980
‫which stands for promise.

23
00:01:03,980 --> 00:01:06,560
‫So remember that this one here returns a promise.

24
00:01:06,560 --> 00:01:08,570
‫Okay, and if we don't await it,

25
00:01:08,570 --> 00:01:11,980
‫well, we will simply save the promise to the variable

26
00:01:11,980 --> 00:01:13,970
‫and not its resolved value.

27
00:01:13,970 --> 00:01:16,763
‫So lets do that here two more times.

28
00:01:18,350 --> 00:01:22,263
‫So one for each random image that we want.

29
00:01:24,200 --> 00:01:26,930
‫Okay, and so like this, we have three promises.

30
00:01:26,930 --> 00:01:29,520
‫Now in order to actually get the resolved values,

31
00:01:29,520 --> 00:01:30,673
‫so the three images,

32
00:01:32,220 --> 00:01:36,430
‫we create a new variable and ow what we do await is

33
00:01:38,210 --> 00:01:40,810
‫promise dot all, okay.

34
00:01:40,810 --> 00:01:44,560
‫And into promise dot all, we pass an array of promises.

35
00:01:44,560 --> 00:01:47,700
‫And so that's going to be res one pro,

36
00:01:47,700 --> 00:01:51,463
‫res two pro, and res three pro.

37
00:01:52,670 --> 00:01:56,350
‫Okay, so again, we have these three promises

38
00:01:56,350 --> 00:01:59,890
‫and these three variables and we can pass an array

39
00:01:59,890 --> 00:02:03,520
‫containing these three promises into promise dot all.

40
00:02:03,520 --> 00:02:06,580
‫And if we then await that, it will basically run

41
00:02:06,580 --> 00:02:09,050
‫these three promises all at the same time

42
00:02:09,050 --> 00:02:11,290
‫and then save to three resolved values

43
00:02:11,290 --> 00:02:14,220
‫into this all array here, okay.

44
00:02:14,220 --> 00:02:17,090
‫So lets actually take a look at that,

45
00:02:17,090 --> 00:02:21,040
‫at that all variable, okay.

46
00:02:21,040 --> 00:02:22,720
‫So this here will probably give us an error now

47
00:02:22,720 --> 00:02:23,883
‫but nevermind that.

48
00:02:25,630 --> 00:02:26,680
‫Yeah, exactly.

49
00:02:26,680 --> 00:02:29,130
‫But what I'm interested in here is to see if the

50
00:02:29,130 --> 00:02:32,210
‫all variable and what I want to show you is that

51
00:02:32,210 --> 00:02:36,370
‫it is indeed an array but I see that it is way too big here

52
00:02:36,370 --> 00:02:38,770
‫because it has all these informations

53
00:02:38,770 --> 00:02:41,630
‫about all these http requests.

54
00:02:41,630 --> 00:02:43,300
‫We're going to take a look at this kind of stuff

55
00:02:43,300 --> 00:02:47,670
‫here later on and we'll note sections.

56
00:02:47,670 --> 00:02:50,380
‫I just can't, ah, here's the beginning.

57
00:02:50,380 --> 00:02:53,600
‫So, you see that it actually starts with this bracket

58
00:02:53,600 --> 00:02:56,070
‫meaning that it is an array, okay.

59
00:02:56,070 --> 00:02:58,810
‫But what we're interested in here is,

60
00:02:58,810 --> 00:03:01,020
‫in each of these elements, we are interested in

61
00:03:01,020 --> 00:03:03,050
‫body dot message, right?

62
00:03:03,050 --> 00:03:05,480
‫So that's what we used to read before

63
00:03:05,480 --> 00:03:07,200
‫from each of the results.

64
00:03:07,200 --> 00:03:10,320
‫So, we need to now create a new array

65
00:03:10,320 --> 00:03:14,710
‫which only contains these body dot messages.

66
00:03:14,710 --> 00:03:15,623
‫Okay, make sense?

67
00:03:17,940 --> 00:03:20,090
‫And lets use maps for that.

68
00:03:20,090 --> 00:03:23,270
‫So images and all

69
00:03:23,270 --> 00:03:24,103
‫dot maps.

70
00:03:24,103 --> 00:03:27,470
‫And I hope that you are familiar with these,

71
00:03:27,470 --> 00:03:28,700
‫all these array methods.

72
00:03:28,700 --> 00:03:32,270
‫So maps will loop through the all array

73
00:03:32,270 --> 00:03:35,960
‫and the safety value that we return in each iteration

74
00:03:35,960 --> 00:03:39,260
‫into a new array called images, okay.

75
00:03:39,260 --> 00:03:41,630
‫And the current value in each iteration

76
00:03:41,630 --> 00:03:44,260
‫is called element and what we will return

77
00:03:44,260 --> 00:03:47,123
‫is element dot body dot message.

78
00:03:48,900 --> 00:03:50,860
‫Okay, so that is basic JavaScript

79
00:03:50,860 --> 00:03:54,760
‫and you should be familiar with that kind of stuff by now.

80
00:03:54,760 --> 00:03:55,593
‫Alright.

81
00:03:56,610 --> 00:04:00,223
‫So, if we now look at images, that should,

82
00:04:02,200 --> 00:04:03,750
‫well, where is that?

83
00:04:03,750 --> 00:04:06,860
‫Oh, all dot maps, oh, of course it's not maps,

84
00:04:06,860 --> 00:04:08,653
‫it's just maps, sorry for that.

85
00:04:11,100 --> 00:04:14,200
‫And here indeed, we have now our array

86
00:04:14,200 --> 00:04:15,860
‫with these three images.

87
00:04:15,860 --> 00:04:18,000
‫So, great, they are three different images.

88
00:04:18,000 --> 00:04:19,210
‫And now all we have to do is to

89
00:04:19,210 --> 00:04:22,060
‫actually save them to our file.

90
00:04:22,060 --> 00:04:24,810
‫So right now we still get res as not defined

91
00:04:24,810 --> 00:04:27,080
‫and that's because we are still attempting to

92
00:04:27,080 --> 00:04:29,130
‫write res dot body dot message.

93
00:04:29,130 --> 00:04:32,473
‫But res does not exist so that's why we get that error.

94
00:04:33,620 --> 00:04:36,110
‫So instead, what we want to write here is

95
00:04:37,129 --> 00:04:40,653
‫a string, basically, containing these three strings.

96
00:04:42,110 --> 00:04:45,823
‫So what we do is images dot join.

97
00:04:48,400 --> 00:04:50,360
‫And we will join them by putting a

98
00:04:50,360 --> 00:04:52,770
‫new line character between them.

99
00:04:52,770 --> 00:04:57,440
‫So back slash n, means a new line, okay.

100
00:04:57,440 --> 00:05:00,750
‫And so that basically will put these three strings,

101
00:05:00,750 --> 00:05:03,430
‫each one, in a new line, alright.

102
00:05:03,430 --> 00:05:07,420
‫So join, joins the three strings into a new string

103
00:05:07,420 --> 00:05:11,520
‫and joins them by using a new line character between them.

104
00:05:11,520 --> 00:05:14,823
‫Okay, and so that should do the trick.

105
00:05:15,670 --> 00:05:16,503
‫Give it a save.

106
00:05:17,760 --> 00:05:21,220
‫And yeah, so now it says random dot image saved to file,

107
00:05:21,220 --> 00:05:22,390
‫no more errors.

108
00:05:22,390 --> 00:05:25,670
‫Let's take a look, dot image, and it did, here we go.

109
00:05:25,670 --> 00:05:27,290
‫And we have three different images.

110
00:05:27,290 --> 00:05:32,010
‫First this one, then this guy, and finally the third one.

111
00:05:32,010 --> 00:05:33,950
‫So, three different dog images.

112
00:05:33,950 --> 00:05:36,200
‫And we got them all at the same time

113
00:05:36,200 --> 00:05:39,830
‫by using promise dot all, okay.

114
00:05:39,830 --> 00:05:43,320
‫I hope that made sense and in fact,

115
00:05:43,320 --> 00:05:46,140
‫I hope that this entire section made sense to you

116
00:05:46,140 --> 00:05:48,680
‫because from now on, I will assume that you

117
00:05:48,680 --> 00:05:51,430
‫know the basics of promises and a single wait.

118
00:05:51,430 --> 00:05:53,290
‫And we'll actually start using them throughout

119
00:05:53,290 --> 00:05:55,860
‫the rest of the course, assuming that you

120
00:05:55,860 --> 00:05:58,830
‫know what we are doing, okay.

121
00:05:58,830 --> 00:06:01,000
‫And Node actually supports a single wait since

122
00:06:01,000 --> 00:06:03,330
‫its version seven point six,

123
00:06:03,330 --> 00:06:05,773
‫and so now we can use it without any problem.

