﻿1
00:00:01,110 --> 00:00:03,390
‫So, in the previous lecture, we learned how

2
00:00:03,390 --> 00:00:06,930
‫to handle fetching data using promise methods.

3
00:00:06,930 --> 00:00:09,420
‫But in this lecture, let's take it one step further

4
00:00:09,420 --> 00:00:13,350
‫and learn how to do the exact same thing with a much nicer

5
00:00:13,350 --> 00:00:16,053
‫and cleaner syntax called async/await.

6
00:00:17,400 --> 00:00:21,295
‫So, this syntax that we just learned here works really well

7
00:00:21,295 --> 00:00:26,040
‫but it's not the most easy thing to work with, I would say.

8
00:00:26,040 --> 00:00:28,812
‫It's not the most beautiful code, in a way.

9
00:00:28,812 --> 00:00:32,694
‫And so, we can basically implement this logic here

10
00:00:32,694 --> 00:00:35,900
‫in a different way using async functions

11
00:00:35,900 --> 00:00:38,370
‫and the await keyword.

12
00:00:38,370 --> 00:00:41,100
‫Now, everything I explained to you in the previous lecture

13
00:00:41,100 --> 00:00:44,490
‫is still 100% valid, and the whole idea

14
00:00:44,490 --> 00:00:47,130
‫behind promises doesn't go away.

15
00:00:47,130 --> 00:00:48,759
‫The only thing that's going to go away

16
00:00:48,759 --> 00:00:52,320
‫is these then handlers right here.

17
00:00:52,320 --> 00:00:56,523
‫So, we're going to again, do it in a cleaner way.

18
00:00:58,470 --> 00:01:00,870
‫So, now instead of just doing it like this,

19
00:01:00,870 --> 00:01:03,393
‫let's create an async function.

20
00:01:04,290 --> 00:01:08,310
‫So, that's simply a function with the async keyword and

21
00:01:08,310 --> 00:01:13,310
‫before it, and then let's call it or get todos.

22
00:01:21,840 --> 00:01:24,722
‫Then let's just grab this part here

23
00:01:24,722 --> 00:01:29,722
‫and I will actually now comment all of this out here.

24
00:01:34,050 --> 00:01:39,050
‫And so now we can, inside the async function, await this.

25
00:01:39,720 --> 00:01:42,780
‫So, await this promise immediately right here.

26
00:01:42,780 --> 00:01:44,970
‫And what will happen then is that actually,

27
00:01:44,970 --> 00:01:48,240
‫inside this async function, JavaScript will not

28
00:01:48,240 --> 00:01:52,635
‫immediately move on to the next line, like it usually does.

29
00:01:52,635 --> 00:01:55,744
‫So, as I explained in the previous lecture, JavaScript,

30
00:01:55,744 --> 00:01:58,890
‫by default, will always immediately move on

31
00:01:58,890 --> 00:02:00,000
‫to the next line.

32
00:02:00,000 --> 00:02:03,660
‫It never waits for anyone, basically.

33
00:02:03,660 --> 00:02:07,350
‫So, here as we fetch, it'll then immediately move here,

34
00:02:07,350 --> 00:02:09,480
‫move here, and move here.

35
00:02:09,480 --> 00:02:13,020
‫It'll, of course, not execute these callback functions,

36
00:02:13,020 --> 00:02:15,870
‫but it will basically register them.

37
00:02:15,870 --> 00:02:18,570
‫That's not really important, but what is important is

38
00:02:18,570 --> 00:02:22,680
‫that we saw that immediately, this code here was locked

39
00:02:22,680 --> 00:02:23,610
‫to the console.

40
00:02:23,610 --> 00:02:27,660
‫So, way before the data from the API actually arrived.

41
00:02:27,660 --> 00:02:31,282
‫But now, with the await keyword inside the async functions,

42
00:02:31,282 --> 00:02:34,816
‫for the first time in JavaScript, we do actually have a way

43
00:02:34,816 --> 00:02:39,323
‫of stopping and of pausing the code inside a function.

44
00:02:39,323 --> 00:02:43,230
‫And by doing this, it makes the function looks a lot more

45
00:02:43,230 --> 00:02:46,278
‫normal again and a lot more like normal,

46
00:02:46,278 --> 00:02:48,363
‫synchronous JavaScript code.

47
00:02:49,260 --> 00:02:53,220
‫Because then we can very simply store the result

48
00:02:53,220 --> 00:02:55,773
‫of this here into a variable.

49
00:02:57,930 --> 00:02:59,621
‫So, let's again call it response

50
00:02:59,621 --> 00:03:02,820
‫and it's the exact same name as before.

51
00:03:02,820 --> 00:03:05,250
‫So, here we call the response a res

52
00:03:05,250 --> 00:03:09,540
‫inside this callback function because, well, the response

53
00:03:09,540 --> 00:03:13,904
‫of the fetch is the data that is passed into this callback.

54
00:03:13,904 --> 00:03:17,430
‫So, basically the response is this res.

55
00:03:17,430 --> 00:03:21,378
‫So here, we can immediately and directly call it like this

56
00:03:21,378 --> 00:03:24,630
‫in a way that is a lot more legible

57
00:03:24,630 --> 00:03:27,033
‫and a lot more easy to understand.

58
00:03:28,350 --> 00:03:30,350
‫And the same thing, then, with the data.

59
00:03:32,670 --> 00:03:34,950
‫So, we can immediately call it data

60
00:03:34,950 --> 00:03:39,900
‫and then simply await res.jsn.

61
00:03:39,900 --> 00:03:44,900
‫And then we can log it to the console just like this.

62
00:03:46,080 --> 00:03:49,203
‫And then we can call that function out here.

63
00:03:53,280 --> 00:03:55,350
‫And there we go.

64
00:03:55,350 --> 00:03:59,823
‫So, here we get all this data that we had before again.

65
00:04:02,100 --> 00:04:04,860
‫But, of course, that waiting only works

66
00:04:04,860 --> 00:04:06,810
‫inside this async function.

67
00:04:06,810 --> 00:04:10,020
‫So, if here we again logged Jonas to the console,

68
00:04:10,020 --> 00:04:12,300
‫you would see that Jonas, of course,

69
00:04:12,300 --> 00:04:14,700
‫needs to be logged again before.

70
00:04:14,700 --> 00:04:18,060
‫Because while JavaScript here simply defines the function,

71
00:04:18,060 --> 00:04:21,060
‫then it derives here, it calls the function

72
00:04:21,060 --> 00:04:24,270
‫and immediately it moves on to the next line.

73
00:04:24,270 --> 00:04:27,180
‫But this function here, while being executed,

74
00:04:27,180 --> 00:04:30,559
‫does actually wait, but only here inside this async function

75
00:04:30,559 --> 00:04:34,473
‫and only in the lines that have this await keyword.

76
00:04:35,490 --> 00:04:39,030
‫So, what do you think would happen if we actually would try

77
00:04:39,030 --> 00:04:42,483
‫to return something from here and store it into a variable?

78
00:04:44,160 --> 00:04:49,050
‫So like, returning these posts? Well, that's not correct.

79
00:04:49,050 --> 00:04:52,113
‫Well, actually, yeah, data. It's called data right here.

80
00:04:53,310 --> 00:04:56,880
‫Now, let's try to store that here into some variable.

81
00:04:56,880 --> 00:04:59,703
‫So, todos equal, get todos.

82
00:05:02,040 --> 00:05:03,930
‫So, what do you think would be logged

83
00:05:03,930 --> 00:05:05,230
‫to the console right here?

84
00:05:06,750 --> 00:05:09,884
‫Well, we can see it here. It is a promise.

85
00:05:09,884 --> 00:05:12,720
‫So, what's going on here?

86
00:05:12,720 --> 00:05:14,793
‫Well, it's the same thing as before.

87
00:05:15,780 --> 00:05:17,790
‫Right after this function is being called,

88
00:05:17,790 --> 00:05:20,640
‫JavaScript immediately moves on to the next line.

89
00:05:20,640 --> 00:05:22,260
‫It does not wait.

90
00:05:22,260 --> 00:05:24,963
‫And so at this point here, JavaScript has no way

91
00:05:24,963 --> 00:05:28,680
‫of knowing yet what these todos are going to be.

92
00:05:28,680 --> 00:05:31,296
‫And so simply, todos is a promise,

93
00:05:31,296 --> 00:05:35,756
‫which means that the result value of this async function

94
00:05:35,756 --> 00:05:38,760
‫is always just a promise.

95
00:05:38,760 --> 00:05:42,480
‫And this is a fundamental piece of knowledge that you need

96
00:05:42,480 --> 00:05:44,908
‫to have in order to work with async functions

97
00:05:44,908 --> 00:05:49,908
‫and with asynchronous data in general in JavaScript.

98
00:05:50,310 --> 00:05:53,144
‫Because now, if you wanted to actually get these todos

99
00:05:53,144 --> 00:05:57,480
‫out here, you would again have to use the then method

100
00:05:57,480 --> 00:05:59,370
‫on here or you would have

101
00:05:59,370 --> 00:06:01,890
‫to use another async/await function.

102
00:06:01,890 --> 00:06:04,770
‫Now, you have another promise in your hands here

103
00:06:04,770 --> 00:06:06,090
‫that you need to handle.

104
00:06:06,090 --> 00:06:09,583
‫But, fortunately for us, in React, that's not very common

105
00:06:09,583 --> 00:06:13,753
‫because usually what we want as soon as we receive some data

106
00:06:13,753 --> 00:06:15,949
‫is to set the state.

107
00:06:15,949 --> 00:06:19,380
‫So, that we would do immediately here in this async function

108
00:06:19,380 --> 00:06:23,160
‫and then we wouldn't usually return anything here.

109
00:06:23,160 --> 00:06:26,280
‫So, this was just again showing you the synchronous

110
00:06:26,280 --> 00:06:29,190
‫and asynchronous nature of JavaScript.

111
00:06:29,190 --> 00:06:31,212
‫So, if you want to understand this a bit better,

112
00:06:31,212 --> 00:06:35,117
‫then you can go ahead and compare these two solutions here,

113
00:06:35,117 --> 00:06:37,323
‫which achieve the exact same thing,

114
00:06:38,280 --> 00:06:42,450
‫and see how they are different and how, at least

115
00:06:42,450 --> 00:06:45,600
‫in my opinion, this one is a lot nicer 'cause it looks,

116
00:06:45,600 --> 00:06:47,373
‫again, like a normal function,

117
00:06:48,330 --> 00:06:50,700
‫while this one here does not really.

118
00:06:50,700 --> 00:06:53,580
‫So, here you can easily store the result

119
00:06:53,580 --> 00:06:56,721
‫of the awaited data immediately into some variables.

120
00:06:56,721 --> 00:06:59,679
‫And yeah, then use them in a flow

121
00:06:59,679 --> 00:07:03,180
‫that looks just like synchronous code.

122
00:07:03,180 --> 00:07:06,596
‫Okay, and with this, we actually finish our review

123
00:07:06,596 --> 00:07:10,830
‫of Essential JavaScript for React section.

124
00:07:10,830 --> 00:07:12,962
‫So, hopefully this brought you up to speed

125
00:07:12,962 --> 00:07:16,950
‫with all the JavaScript that you need to know to be able

126
00:07:16,950 --> 00:07:19,710
‫to now successfully complete this course.

127
00:07:19,710 --> 00:07:23,040
‫And so, now the time has actually come to really move

128
00:07:23,040 --> 00:07:27,423
‫onto the actual complete and ultimate React course.

