1
00:00:02,130 --> 00:00:03,860
Now we're almost done,

2
00:00:03,860 --> 00:00:05,880
but there is one last feature

3
00:00:05,880 --> 00:00:08,610
which you must know as a developer when working

4
00:00:08,610 --> 00:00:12,790
with async code and Promises, and that's important.

5
00:00:12,790 --> 00:00:16,430
This concept now only applies to Promises.

6
00:00:16,430 --> 00:00:18,160
Promises already allow us

7
00:00:18,160 --> 00:00:20,840
to write code a little bit more structured

8
00:00:20,840 --> 00:00:23,550
than having multiple callback functions

9
00:00:23,550 --> 00:00:27,180
that potentially might be nested into each other.

10
00:00:27,180 --> 00:00:31,430
Still, we have to write these extra Then methods.

11
00:00:31,430 --> 00:00:34,340
And now let's assume we have a function.

12
00:00:34,340 --> 00:00:36,920
Like this readFile function here

13
00:00:36,920 --> 00:00:41,920
that performs only one long taking asynchronous operation,

14
00:00:42,500 --> 00:00:46,020
or maybe multiple operations stats depend on each other,

15
00:00:46,020 --> 00:00:49,520
but not multiple operations that should be started

16
00:00:49,520 --> 00:00:51,093
at the same point of time.

17
00:00:52,180 --> 00:00:55,120
If that's the case, if we only have one operation,

18
00:00:55,120 --> 00:00:58,140
or if we have dependent operations,

19
00:00:58,140 --> 00:01:01,160
then we still might have some async code

20
00:01:01,160 --> 00:01:02,480
because maybe the package,

21
00:01:02,480 --> 00:01:04,940
we're using at the operation we're performing,

22
00:01:04,940 --> 00:01:07,950
doesn't give us async version of the method

23
00:01:07,950 --> 00:01:10,340
as readFile did before.

24
00:01:10,340 --> 00:01:14,400
There we always had readFileSync as an alternative,

25
00:01:14,400 --> 00:01:16,370
but that's pretty much an exception.

26
00:01:16,370 --> 00:01:20,430
Most packages don't give us such a sync alternative

27
00:01:20,430 --> 00:01:22,750
when doing some async work,

28
00:01:22,750 --> 00:01:25,780
and therefore we might be stuck with using a promise

29
00:01:25,780 --> 00:01:27,620
or a callback function.

30
00:01:27,620 --> 00:01:30,030
Even though we might know that the code,

31
00:01:30,030 --> 00:01:31,430
after reading the file,

32
00:01:31,430 --> 00:01:34,620
should only execute once that's done.

33
00:01:34,620 --> 00:01:36,933
So we don't want to execute it earlier.

34
00:01:37,900 --> 00:01:40,140
Well, of course we can then move that code here

35
00:01:40,140 --> 00:01:41,530
into this Then block,

36
00:01:41,530 --> 00:01:45,110
but still we have to write all that extra Then logic,

37
00:01:45,110 --> 00:01:47,793
and we have to deal with those callback functions.

38
00:01:48,730 --> 00:01:52,450
In scenarios like this, you can simplify your code

39
00:01:52,450 --> 00:01:56,100
with a feature called 'async await'.

40
00:01:56,100 --> 00:01:57,223
Now, what is that?

41
00:01:58,110 --> 00:02:00,780
With that feature, you can turn a function

42
00:02:00,780 --> 00:02:03,310
into a so-called async function

43
00:02:03,310 --> 00:02:05,973
by adding the async keyword in front of it.

44
00:02:07,500 --> 00:02:08,940
Once you do that,

45
00:02:08,940 --> 00:02:12,600
this function will automatically return a promise

46
00:02:12,600 --> 00:02:16,280
without you explicitly returning one.

47
00:02:16,280 --> 00:02:19,050
I have no return statement in this function

48
00:02:19,050 --> 00:02:20,960
and yet at readFile,

49
00:02:20,960 --> 00:02:23,560
this function will return a promise now

50
00:02:23,560 --> 00:02:24,743
because of async.

51
00:02:26,420 --> 00:02:31,350
Now the async keyword, however, unlocks one other key word,

52
00:02:31,350 --> 00:02:35,080
which we can now use inside of that async function.

53
00:02:35,080 --> 00:02:37,800
And that's the await keyword.

54
00:02:37,800 --> 00:02:41,710
We can add that keyword in front of any method

55
00:02:41,710 --> 00:02:43,490
that returns a promise.

56
00:02:43,490 --> 00:02:46,290
Like readFile does here now.

57
00:02:46,290 --> 00:02:49,410
When we do that, when we do add a weight

58
00:02:49,410 --> 00:02:52,330
we can get rid of Then,

59
00:02:52,330 --> 00:02:56,830
and instead store that result in fileData.

60
00:02:56,830 --> 00:02:57,720
Whoops,

61
00:02:57,720 --> 00:03:00,690
in that fileData variable, either find here.

62
00:03:00,690 --> 00:03:02,560
Just as I did it before.

63
00:03:02,560 --> 00:03:05,380
Even though that's not readFile sync,

64
00:03:05,380 --> 00:03:08,083
but readFile which returns a promise.

65
00:03:09,066 --> 00:03:12,330
But under the hood, when using async await,

66
00:03:12,330 --> 00:03:16,970
JavaScript will add Then to that promise for you,

67
00:03:16,970 --> 00:03:19,230
and it will provide the value

68
00:03:19,230 --> 00:03:22,990
which you would otherwise get here as a parameter value

69
00:03:22,990 --> 00:03:25,920
in this function that's passed to Then,

70
00:03:25,920 --> 00:03:28,730
to this variable or constant here.

71
00:03:28,730 --> 00:03:31,490
So JavaScript will give you that value

72
00:03:31,490 --> 00:03:33,320
as a return value here

73
00:03:33,320 --> 00:03:36,950
just as if this would be asynchronous operation,

74
00:03:36,950 --> 00:03:38,490
even though it isn't.

75
00:03:38,490 --> 00:03:40,680
But it's transformed under the hood,

76
00:03:40,680 --> 00:03:42,303
behind the scene so to say.

77
00:03:43,760 --> 00:03:44,593
Therefore,

78
00:03:44,593 --> 00:03:46,813
you can then get rid of Then here,

79
00:03:48,560 --> 00:03:50,530
and just execute your other code

80
00:03:50,530 --> 00:03:52,650
in the very next lines thereafter,

81
00:03:52,650 --> 00:03:57,150
and code execution will stop until this line here.

82
00:03:57,150 --> 00:03:59,160
Line 15 is done

83
00:03:59,160 --> 00:04:01,973
before it advances to the next lines.

84
00:04:03,050 --> 00:04:05,363
And let me get rid of this part here now.

85
00:04:06,800 --> 00:04:09,130
Now it is might sound like a step back

86
00:04:09,130 --> 00:04:12,130
after all we started using async code

87
00:04:12,130 --> 00:04:14,870
so that we can execute multiple operations

88
00:04:14,870 --> 00:04:16,060
at the same time.

89
00:04:16,060 --> 00:04:17,490
But as I mentioned before,

90
00:04:17,490 --> 00:04:19,709
if you don't have that use case

91
00:04:19,709 --> 00:04:22,060
and still you are using some operation

92
00:04:22,060 --> 00:04:24,700
that forces you to use a promise,

93
00:04:24,700 --> 00:04:27,530
then you can simplify your code again

94
00:04:27,530 --> 00:04:29,663
with async await like that.

95
00:04:31,302 --> 00:04:33,610
Now it looks like synchronous code,

96
00:04:33,610 --> 00:04:34,800
but under the hood,

97
00:04:34,800 --> 00:04:35,860
that's important,

98
00:04:35,860 --> 00:04:40,270
it is still translated to this Then version of the code.

99
00:04:40,270 --> 00:04:43,920
So to that version you saw a couple of minutes ago.

100
00:04:43,920 --> 00:04:45,500
But to us as a developer,

101
00:04:45,500 --> 00:04:48,660
this code is now easier to read and write,

102
00:04:48,660 --> 00:04:51,460
and it looks like synchronous code to us,

103
00:04:51,460 --> 00:04:54,623
even though under the hood, it's still asynchronous.

104
00:04:55,470 --> 00:04:59,550
This code here in the end is just put into a Then block

105
00:04:59,550 --> 00:05:02,880
into a when method called and the anonymous function

106
00:05:02,880 --> 00:05:07,000
that's passed to then automatically by Java script.

107
00:05:07,000 --> 00:05:08,800
That's how you can think about that.

108
00:05:09,690 --> 00:05:12,180
And if you now want to handle errors again,

109
00:05:12,180 --> 00:05:15,470
since this now again looks like synchronous code.

110
00:05:15,470 --> 00:05:19,020
You can again use Try Catch as before

111
00:05:19,020 --> 00:05:23,580
that did not work on the asynchronous code before

112
00:05:23,580 --> 00:05:26,040
with Callbacks or Promises.

113
00:05:26,040 --> 00:05:28,420
But when you start using Async Await,

114
00:05:28,420 --> 00:05:31,623
then Try Catch is available again.

115
00:05:32,530 --> 00:05:34,960
So that's how you can then handle

116
00:05:34,960 --> 00:05:37,263
that error you might be getting.

117
00:05:38,170 --> 00:05:40,540
As a side note if you want to get access

118
00:05:40,540 --> 00:05:42,070
to the error object,

119
00:05:42,070 --> 00:05:45,168
that's produced automatically by a readFile

120
00:05:45,168 --> 00:05:47,020
in case things go wrong.

121
00:05:47,020 --> 00:05:49,640
You can add a parameter.

122
00:05:49,640 --> 00:05:53,050
So to say, after a Catch and that will then be available

123
00:05:53,050 --> 00:05:54,570
in this Catch block.

124
00:05:54,570 --> 00:05:57,770
And that will be that error object that is produced

125
00:05:57,770 --> 00:05:59,640
by a readFile.

126
00:05:59,640 --> 00:06:03,270
That would have been available here before as well

127
00:06:03,270 --> 00:06:05,330
When we used readFileSync,

128
00:06:05,330 --> 00:06:09,133
and it's now available again when using async await.

129
00:06:10,710 --> 00:06:15,090
And that's now the last concept related to async code.

130
00:06:15,090 --> 00:06:17,500
It allows us to go back to a style

131
00:06:17,500 --> 00:06:19,680
that looks like synchronous code,

132
00:06:19,680 --> 00:06:23,240
even though we're still having some async operation.

133
00:06:23,240 --> 00:06:25,800
And async await can be very useful

134
00:06:25,800 --> 00:06:28,510
if you want to have that more simplified code,

135
00:06:28,510 --> 00:06:32,390
And you're fine with not starting multiple async operations

136
00:06:32,390 --> 00:06:33,830
at the same time,

137
00:06:33,830 --> 00:06:35,770
because that won't be possible

138
00:06:35,770 --> 00:06:36,603
because,

139
00:06:36,603 --> 00:06:37,436
as mentioned,

140
00:06:37,436 --> 00:06:40,860
Await, now in the end blocks code execution, again,

141
00:06:40,860 --> 00:06:42,890
until this is done.

142
00:06:42,890 --> 00:06:45,650
But if you don't need to start multiple operations

143
00:06:45,650 --> 00:06:48,870
at the same time, then this is a simplification

144
00:06:48,870 --> 00:06:50,113
that can be useful.

