﻿1
00:00:01,500 --> 00:00:03,600
‫{\an8}In this video, we will promisify

2
00:00:03,600 --> 00:00:06,680
‫{\an8}the read file and write file functions.

3
00:00:06,680 --> 00:00:09,440
‫{\an8}Which means that we will make them so

4
00:00:09,440 --> 00:00:11,230
‫{\an8}that they return promises

5
00:00:11,230 --> 00:00:14,383
‫{\an8}instead of us passing a callback functions into them.

6
00:00:16,180 --> 00:00:19,156
‫So if you want, it's basically a read file function

7
00:00:19,156 --> 00:00:20,890
‫that returns a promise

8
00:00:20,890 --> 00:00:24,300
‫and that only receives a file name, no callback.

9
00:00:24,300 --> 00:00:26,023
‫So let's do exactly that.

10
00:00:28,530 --> 00:00:33,530
‫So, we're gonna create a read file pro function.

11
00:00:34,110 --> 00:00:36,380
‫So pro for promise because again

12
00:00:36,380 --> 00:00:37,970
‫it returns a promise.

13
00:00:37,970 --> 00:00:40,270
‫And then all we do is to pass in the file name

14
00:00:41,590 --> 00:00:44,713
‫and as we were saying, return a promise.

15
00:00:46,060 --> 00:00:49,570
‫So, return new promise.

16
00:00:49,570 --> 00:00:52,150
‫So we use the promise constructor here

17
00:00:52,150 --> 00:00:55,540
‫which was introduced into the language with ES6.

18
00:00:55,540 --> 00:00:58,600
‫So before ES6 like ES5, ES4

19
00:00:58,600 --> 00:01:00,030
‫and all the previous versions,

20
00:01:00,030 --> 00:01:03,820
‫there was no support for promises in JavaScript.

21
00:01:03,820 --> 00:01:05,670
‫But they changed that in ES6

22
00:01:05,670 --> 00:01:08,660
‫and made the language a lot better in that regard.

23
00:01:08,660 --> 00:01:12,010
‫So regarding asynchronous JavaScript.

24
00:01:12,010 --> 00:01:14,350
‫Now this promise constructor here

25
00:01:14,350 --> 00:01:17,400
‫takes in a so called executor function,

26
00:01:17,400 --> 00:01:20,980
‫which will get called immediately when promise is created.

27
00:01:20,980 --> 00:01:24,450
‫And this function will take in two arguments

28
00:01:24,450 --> 00:01:26,363
‫so resolve and reject.

29
00:01:28,330 --> 00:01:30,370
‫So resolve and reject,

30
00:01:30,370 --> 00:01:32,770
‫but for now don't worry for them,

31
00:01:32,770 --> 00:01:35,020
‫we're gonna be talking about them in a minute.

32
00:01:35,020 --> 00:01:38,550
‫For now let's just focus on this executor function itself

33
00:01:38,550 --> 00:01:41,470
‫because this is where we actually do

34
00:01:41,470 --> 00:01:43,450
‫all this asynchronous work.

35
00:01:43,450 --> 00:01:46,430
‫In this case the read file work.

36
00:01:46,430 --> 00:01:48,890
‫So this is where we're gonna call fs.readfile

37
00:01:51,040 --> 00:01:53,287
‫with the file name that was passed in

38
00:01:53,287 --> 00:01:57,270
‫and then the usual callback function, okay.

39
00:01:57,270 --> 00:02:00,660
‫So the error and data,

40
00:02:00,660 --> 00:02:02,000
‫so just like before.

41
00:02:02,000 --> 00:02:03,550
‫So keep in mind that we're not actually

42
00:02:03,550 --> 00:02:06,200
‫changing the read file function

43
00:02:06,200 --> 00:02:07,997
‫we're simply creating a new function

44
00:02:07,997 --> 00:02:11,140
‫which behind the scenes of course still runs

45
00:02:11,140 --> 00:02:13,850
‫the built in read file function

46
00:02:13,850 --> 00:02:15,570
‫but then returns a promise

47
00:02:15,570 --> 00:02:17,040
‫so that we can use the promise

48
00:02:17,040 --> 00:02:19,170
‫instead of the callback function.

49
00:02:19,170 --> 00:02:20,320
‫Okay.

50
00:02:20,320 --> 00:02:22,630
‫So we have that read file function here

51
00:02:22,630 --> 00:02:24,270
‫which will do its work

52
00:02:24,270 --> 00:02:27,250
‫and when it's ready will come back with the data.

53
00:02:27,250 --> 00:02:29,070
‫And now this is where the resolve

54
00:02:29,070 --> 00:02:31,510
‫and the reject functions come into play.

55
00:02:31,510 --> 00:02:34,610
‫Because both these arguments that are available

56
00:02:34,610 --> 00:02:38,390
‫in the executor function they are both functions.

57
00:02:38,390 --> 00:02:40,410
‫Now calling the resolve function

58
00:02:40,410 --> 00:02:44,250
‫will basically mark the promise as successful

59
00:02:44,250 --> 00:02:45,320
‫so as fulfilled,

60
00:02:45,320 --> 00:02:49,010
‫and return the successful value from the promise.

61
00:02:49,010 --> 00:02:50,310
‫So let's actually do that.

62
00:02:51,520 --> 00:02:55,270
‫So we call resolve with data.

63
00:02:55,270 --> 00:02:58,690
‫So whatever variable that we pass into the resolve function

64
00:02:58,690 --> 00:03:00,860
‫is what will be later available

65
00:03:00,860 --> 00:03:04,370
‫as the argument in the then method, all right?

66
00:03:04,370 --> 00:03:07,310
‫So this data here will be the value

67
00:03:07,310 --> 00:03:10,550
‫that this promise returns to us, okay.

68
00:03:10,550 --> 00:03:12,930
‫That probably sounds a bit confusing, right?

69
00:03:12,930 --> 00:03:17,080
‫So again, whatever we pass into this function here,

70
00:03:17,080 --> 00:03:18,840
‫into the resolve function,

71
00:03:18,840 --> 00:03:20,690
‫is the result of the promise

72
00:03:20,690 --> 00:03:23,590
‫that will be available in the then handler.

73
00:03:23,590 --> 00:03:24,700
‫Now on the other hand,

74
00:03:24,700 --> 00:03:27,800
‫we can also mark the promise as rejected

75
00:03:27,800 --> 00:03:29,960
‫in case there was an error, okay.

76
00:03:29,960 --> 00:03:31,230
‫And we do that by calling,

77
00:03:31,230 --> 00:03:32,450
‫as you can probably guess,

78
00:03:32,450 --> 00:03:34,710
‫the reject function.

79
00:03:34,710 --> 00:03:36,390
‫So, if there was an error

80
00:03:38,300 --> 00:03:40,530
‫and this is similar to what we did before.

81
00:03:40,530 --> 00:03:42,520
‫So if there was an error here,

82
00:03:42,520 --> 00:03:45,170
‫then call the reject function.

83
00:03:45,170 --> 00:03:47,770
‫And whatever we pass into this one

84
00:03:47,770 --> 00:03:50,310
‫will be the error that is later available

85
00:03:50,310 --> 00:03:52,930
‫in the catch method.

86
00:03:52,930 --> 00:03:54,810
‫So, in this one here.

87
00:03:54,810 --> 00:03:56,110
‫Okay?

88
00:03:56,110 --> 00:03:58,673
‫So let's just pass a string in here,

89
00:04:00,200 --> 00:04:05,060
‫I could not find that file,

90
00:04:05,060 --> 00:04:08,480
‫some emoji here maybe to make it pop a bit more

91
00:04:08,480 --> 00:04:11,030
‫in our console.logs, all right?

92
00:04:11,030 --> 00:04:11,993
‫And, okay.

93
00:04:11,993 --> 00:04:14,070
‫That is actually already it.

94
00:04:14,070 --> 00:04:15,940
‫So, let's review this here again.

95
00:04:15,940 --> 00:04:17,850
‫So we created a new file,

96
00:04:17,850 --> 00:04:21,080
‫in there we will pass a file name.

97
00:04:21,080 --> 00:04:22,200
‫So this argument.

98
00:04:22,200 --> 00:04:25,580
‫And all we do here is to return this promise.

99
00:04:25,580 --> 00:04:29,160
‫This promise takes in one executor function,

100
00:04:29,160 --> 00:04:30,560
‫so this one here,

101
00:04:30,560 --> 00:04:34,040
‫which is where we do our asynchronous work.

102
00:04:34,040 --> 00:04:36,110
‫In that case here of course that is our

103
00:04:36,110 --> 00:04:36,943
‫fs.readfile.

104
00:04:38,500 --> 00:04:41,250
‫Then if we get our data in a successful way

105
00:04:41,250 --> 00:04:42,810
‫we call the resolve function

106
00:04:42,810 --> 00:04:44,950
‫with the resolve value.

107
00:04:44,950 --> 00:04:46,290
‫And if there is an error

108
00:04:46,290 --> 00:04:49,040
‫we call the reject function, okay?

109
00:04:49,040 --> 00:04:52,560
‫And so let's now actually use this in practice.

110
00:04:52,560 --> 00:04:55,030
‫Read file pro,

111
00:04:55,030 --> 00:04:57,490
‫we pass in our file name.

112
00:04:57,490 --> 00:04:58,890
‫So that's this one of course

113
00:05:00,020 --> 00:05:01,300
‫and that's it.

114
00:05:01,300 --> 00:05:03,460
‫So this will now return a promise.

115
00:05:03,460 --> 00:05:04,980
‫And so just like before

116
00:05:04,980 --> 00:05:08,203
‫we can then use our then method on that.

117
00:05:11,670 --> 00:05:14,930
‫Okay and so this data argument here

118
00:05:14,930 --> 00:05:17,890
‫will be exactly what we returned from the promise

119
00:05:17,890 --> 00:05:19,510
‫in case it was successful.

120
00:05:19,510 --> 00:05:21,540
‫So, with this resolve.

121
00:05:21,540 --> 00:05:23,740
‫So right now our dog file here

122
00:05:23,740 --> 00:05:25,540
‫contains labrador

123
00:05:25,540 --> 00:05:29,440
‫and so this data here will be labrador.

124
00:05:29,440 --> 00:05:31,280
‫And so that is then later available

125
00:05:31,280 --> 00:05:32,737
‫in our then handler here.

126
00:05:32,737 --> 00:05:36,230
‫But of course it doesn't need to have the same name,

127
00:05:36,230 --> 00:05:38,670
‫I could also call it like result or something

128
00:05:38,670 --> 00:05:41,300
‫it doesn't matter, all right?

129
00:05:41,300 --> 00:05:44,080
‫Okay, so let's now take all of this

130
00:05:44,920 --> 00:05:46,463
‫and actually put it here.

131
00:05:48,270 --> 00:05:49,103
‫Okay.

132
00:05:49,103 --> 00:05:52,000
‫Get rid of all of this, save it

133
00:05:52,000 --> 00:05:53,720
‫and that will run it.

134
00:05:53,720 --> 00:05:55,520
‫Now we get that error down here

135
00:05:55,520 --> 00:05:57,100
‫so data's not defined.

136
00:05:57,100 --> 00:05:59,480
‫That's because I changed it here to a result.

137
00:05:59,480 --> 00:06:02,210
‫So let's actually go back and change it to data

138
00:06:02,210 --> 00:06:03,270
‫because here we have data

139
00:06:03,270 --> 00:06:04,300
‫and here we have data

140
00:06:04,300 --> 00:06:06,610
‫and so this way I don't have to go ahead

141
00:06:06,610 --> 00:06:09,210
‫and change all of these variable names.

142
00:06:09,210 --> 00:06:10,690
‫Give it another save

143
00:06:10,690 --> 00:06:12,080
‫and here we go.

144
00:06:12,080 --> 00:06:13,460
‫So it worked, you see?

145
00:06:13,460 --> 00:06:15,150
‫We actually got the breed,

146
00:06:15,150 --> 00:06:16,720
‫which is still labrador

147
00:06:16,720 --> 00:06:19,870
‫and this time from our promise, okay.

148
00:06:19,870 --> 00:06:21,150
‫So, this really worked.

149
00:06:21,150 --> 00:06:23,290
‫So our read file pro function

150
00:06:23,290 --> 00:06:24,890
‫now returns a promise

151
00:06:24,890 --> 00:06:29,890
‫and that data here is then available on our then handler.

152
00:06:29,910 --> 00:06:31,660
‫Great, so it's already working

153
00:06:31,660 --> 00:06:33,110
‫and we can make it a lot better

154
00:06:33,110 --> 00:06:33,943
‫and we're gonna do that

155
00:06:33,943 --> 00:06:36,487
‫but for now I also want to promisify

156
00:06:36,487 --> 00:06:39,170
‫this write file function here.

157
00:06:39,170 --> 00:06:40,580
‫So basically, do the same as we did

158
00:06:40,580 --> 00:06:42,520
‫with the read file function.

159
00:06:42,520 --> 00:06:45,110
‫Now, if you're already know a bit about no js,

160
00:06:45,110 --> 00:06:49,000
‫maybe you're thinking that there is actually a function node

161
00:06:49,000 --> 00:06:52,090
‫that will automatically promisify functions for us.

162
00:06:52,090 --> 00:06:54,300
‫But of course I wanted to show you here

163
00:06:54,300 --> 00:06:56,130
‫how promises actually work

164
00:06:56,130 --> 00:06:57,320
‫and how we build them

165
00:06:57,320 --> 00:07:01,270
‫and so that's why I'm doing all of this here, okay.

166
00:07:01,270 --> 00:07:03,860
‫So I really want you to learn about promises,

167
00:07:03,860 --> 00:07:05,410
‫not just how we consume them

168
00:07:05,410 --> 00:07:06,710
‫but also how to build them.

169
00:07:06,710 --> 00:07:08,733
‫And so that's why I'm doing it this way.

170
00:07:11,170 --> 00:07:14,913
‫So, write file and as a promise.

171
00:07:16,720 --> 00:07:19,460
‫And this one actually doesn't only need the file name

172
00:07:19,460 --> 00:07:22,993
‫but also the data that should be written to that file.

173
00:07:25,350 --> 00:07:27,460
‫But then the logic in here is quite similar.

174
00:07:27,460 --> 00:07:30,253
‫So again, we return a new promise,

175
00:07:31,760 --> 00:07:34,230
‫in there we have our executor function

176
00:07:34,230 --> 00:07:37,823
‫which takes resolve and reject as the argument.

177
00:07:39,090 --> 00:07:40,470
‫And these are kind of standard names

178
00:07:40,470 --> 00:07:41,860
‫So resolve and reject,

179
00:07:41,860 --> 00:07:44,000
‫you could call them whatever you really wanted

180
00:07:44,000 --> 00:07:46,463
‫but this is kind of the standard, okay.

181
00:07:47,780 --> 00:07:48,613
‫So fs,

182
00:07:50,211 --> 00:07:51,044
‫write file,

183
00:07:52,870 --> 00:07:54,390
‫we pass in the file,

184
00:07:54,390 --> 00:07:57,523
‫the data and then the callback function.

185
00:07:58,580 --> 00:08:01,260
‫And now something very similar to before,

186
00:08:01,260 --> 00:08:02,800
‫so if there was an error,

187
00:08:02,800 --> 00:08:05,570
‫then call the reject function

188
00:08:05,570 --> 00:08:07,473
‫and pass in some message there.

189
00:08:10,550 --> 00:08:12,340
‫Could not write the file,

190
00:08:12,340 --> 00:08:16,060
‫again with this emoji here to make it stand out a bit more

191
00:08:16,060 --> 00:08:18,870
‫and in case everything worked fine,

192
00:08:18,870 --> 00:08:20,830
‫well then just resolve.

193
00:08:20,830 --> 00:08:24,960
‫Now we don't have any data actually to pass in here right?

194
00:08:24,960 --> 00:08:27,940
‫So we're just gonna pass in some random string

195
00:08:27,940 --> 00:08:30,920
‫something just like saying success, okay.

196
00:08:30,920 --> 00:08:32,770
‫So a promise doesn't always

197
00:08:32,770 --> 00:08:36,000
‫have to return a meaningful value.

198
00:08:36,000 --> 00:08:38,060
‫In this case all we're really trying to do here

199
00:08:38,060 --> 00:08:39,030
‫is to write a file

200
00:08:39,030 --> 00:08:42,340
‫which doesn't really return a meaningful value.

201
00:08:42,340 --> 00:08:46,290
‫So we're now ready to use this promisified function

202
00:08:46,290 --> 00:08:47,280
‫but before we do that,

203
00:08:47,280 --> 00:08:49,640
‫let's actually implement the chaining

204
00:08:49,640 --> 00:08:51,750
‫that I was talking to you about before.

205
00:08:51,750 --> 00:08:52,780
‫So right now,

206
00:08:52,780 --> 00:08:56,170
‫we still in fact have callbacks inside of callbacks

207
00:08:56,170 --> 00:08:58,290
‫inside of callbacks, right?

208
00:08:58,290 --> 00:08:59,550
‫So we have this then,

209
00:08:59,550 --> 00:09:00,930
‫which has this callback.

210
00:09:00,930 --> 00:09:03,800
‫And then in there there is another then handler

211
00:09:03,800 --> 00:09:06,470
‫which has yet another callback in it.

212
00:09:06,470 --> 00:09:09,230
‫So that doesn't really change anything, does it?

213
00:09:09,230 --> 00:09:12,697
‫Instead we want to chain these then handlers here.

214
00:09:12,697 --> 00:09:14,090
‫And the secret to doing that

215
00:09:14,090 --> 00:09:17,670
‫is to make each handler return a new promise.

216
00:09:17,670 --> 00:09:19,280
‫So how are we gonna do that?

217
00:09:19,280 --> 00:09:24,280
‫Well, remember that this function here returns a promise.

218
00:09:24,760 --> 00:09:26,230
‫And so all we have to do is to return

219
00:09:26,230 --> 00:09:29,290
‫that promise from this first then handler.

220
00:09:29,290 --> 00:09:30,490
‫Let me show that to you.

221
00:09:32,370 --> 00:09:35,260
‫So we say return superagent.get

222
00:09:35,260 --> 00:09:38,630
‫and then the string and that is now a promise, okay.

223
00:09:38,630 --> 00:09:41,920
‫And then we actually close this callback function here

224
00:09:41,920 --> 00:09:46,520
‫and chain this then method right onto it.

225
00:09:46,520 --> 00:09:49,400
‫Give it a save, now we have some error here

226
00:09:49,400 --> 00:09:51,830
‫and that is on line 40.

227
00:09:51,830 --> 00:09:53,780
‫Let's get rid of this here

228
00:09:53,780 --> 00:09:54,913
‫and okay.

229
00:09:55,760 --> 00:09:57,730
‫It changed the formatting here for us again

230
00:09:57,730 --> 00:09:59,200
‫that was prettier

231
00:09:59,200 --> 00:10:02,700
‫and then we actually already have it working down here.

232
00:10:02,700 --> 00:10:04,420
‫So why does this work again?

233
00:10:04,420 --> 00:10:06,800
‫Well, of course this function

234
00:10:06,800 --> 00:10:09,130
‫that we created in the beginning returns a promise.

235
00:10:09,130 --> 00:10:11,840
‫So on that we can use the then method.

236
00:10:11,840 --> 00:10:14,440
‫We then make this callback function that's in it

237
00:10:14,440 --> 00:10:16,690
‫again return a promise.

238
00:10:16,690 --> 00:10:18,530
‫And like that we can then chain

239
00:10:18,530 --> 00:10:21,150
‫the next then handler on that.

240
00:10:21,150 --> 00:10:22,860
‫So this result variable here

241
00:10:22,860 --> 00:10:25,760
‫will then be the resolved value of this promise

242
00:10:25,760 --> 00:10:29,170
‫that returned from the previous handler, okay?

243
00:10:29,170 --> 00:10:30,750
‫Makes sense?

244
00:10:30,750 --> 00:10:33,020
‫Now to take it one step further

245
00:10:33,020 --> 00:10:33,990
‫we will go ahead

246
00:10:33,990 --> 00:10:38,423
‫and use our write file pro function here now as well.

247
00:10:39,350 --> 00:10:41,980
‫And since we want to keep chaining the then methods

248
00:10:41,980 --> 00:10:44,190
‫we will again return that actually.

249
00:10:44,190 --> 00:10:48,163
‫So return, write file pro

250
00:10:50,420 --> 00:10:53,513
‫pass in the file name, which is this one,

251
00:10:55,840 --> 00:10:57,070
‫the data...

252
00:11:00,210 --> 00:11:01,360
‫And that's actually it.

253
00:11:03,460 --> 00:11:06,600
‫Okay, so we don't need any of this here anymore,

254
00:11:06,600 --> 00:11:08,250
‫let's just comment it up for now.

255
00:11:09,880 --> 00:11:11,650
‫Get rid of this space here

256
00:11:11,650 --> 00:11:14,973
‫and then on there we can chain the next then handler.

257
00:11:16,010 --> 00:11:18,230
‫So then, and remember that in here

258
00:11:18,230 --> 00:11:21,950
‫we didn't really have any meaningful resolved value.

259
00:11:21,950 --> 00:11:24,240
‫So, we don't want any argument

260
00:11:24,240 --> 00:11:26,580
‫to this callback function here

261
00:11:26,580 --> 00:11:31,433
‫all we want to do is to log this string, right?

262
00:11:32,640 --> 00:11:34,970
‫So now we can actually really get rid of this

263
00:11:36,450 --> 00:11:37,920
‫and okay.

264
00:11:37,920 --> 00:11:39,490
‫So, down here we see now

265
00:11:39,490 --> 00:11:41,090
‫that it's actually already working.

266
00:11:41,090 --> 00:11:43,080
‫And again the trick for being able

267
00:11:43,080 --> 00:11:46,050
‫to chain all of these then methods here

268
00:11:46,050 --> 00:11:49,360
‫is to return a promise before calling each of them.

269
00:11:49,360 --> 00:11:52,360
‫So this one here obviously returns a promise here

270
00:11:52,360 --> 00:11:55,320
‫so then we can chain the then method on it.

271
00:11:55,320 --> 00:11:56,860
‫Then this piece of code here

272
00:11:56,860 --> 00:11:58,110
‫will return a promise

273
00:11:58,110 --> 00:12:00,630
‫and so we can use then on it.

274
00:12:00,630 --> 00:12:02,920
‫Then this piece of code will return a promise

275
00:12:02,920 --> 00:12:07,320
‫and so we can use then on it again, okay?

276
00:12:07,320 --> 00:12:09,010
‫So that is the secret to it.

277
00:12:09,010 --> 00:12:10,380
‫Give it a save again

278
00:12:10,380 --> 00:12:13,500
‫and so down here we see that the breed is still labrador.

279
00:12:13,500 --> 00:12:16,640
‫We see that this here is the link,

280
00:12:16,640 --> 00:12:19,180
‫let's go here and confirm

281
00:12:19,180 --> 00:12:21,300
‫that this is here indeed the same as this.

282
00:12:21,300 --> 00:12:26,050
‫And so it did successfully write the string to the text file

283
00:12:26,050 --> 00:12:30,717
‫using our promisified write file function, okay.

284
00:12:30,717 --> 00:12:34,240
‫And then in the end we have this error handler.

285
00:12:34,240 --> 00:12:38,060
‫And the beauty here is that for all of these chains

286
00:12:38,060 --> 00:12:39,430
‫then handlers in the end,

287
00:12:39,430 --> 00:12:43,020
‫we simply need one single catch handler.

288
00:12:43,020 --> 00:12:44,980
‫So one function handling the errors

289
00:12:44,980 --> 00:12:48,910
‫coming from either one of the three promises, okay.

290
00:12:48,910 --> 00:12:52,090
‫Let's actually only log the error to the console

291
00:12:52,090 --> 00:12:53,900
‫and let's try to, for example,

292
00:12:53,900 --> 00:12:56,160
‫change this file here.

293
00:12:56,160 --> 00:12:57,980
‫Actually not the file,

294
00:12:57,980 --> 00:13:01,940
‫but I'm gonna change the file name here,

295
00:13:01,940 --> 00:13:06,150
‫save it and now we see I could not find that file.

296
00:13:06,150 --> 00:13:07,970
‫So where does that come from?

297
00:13:07,970 --> 00:13:09,720
‫It comes right up here

298
00:13:09,720 --> 00:13:13,410
‫from calling the reject function, okay?

299
00:13:13,410 --> 00:13:16,320
‫So we have an error here because we couldn't find the file

300
00:13:16,320 --> 00:13:18,890
‫and so we called the reject function.

301
00:13:18,890 --> 00:13:22,600
‫And that reject function will then mark this promise here

302
00:13:23,850 --> 00:13:25,720
‫so remember this is a promise.

303
00:13:25,720 --> 00:13:27,780
‫It will mark it as rejected.

304
00:13:27,780 --> 00:13:30,870
‫And therefore, the catch function is called.

305
00:13:30,870 --> 00:13:33,260
‫And so then we log that error to the console.

306
00:13:33,260 --> 00:13:36,313
‫And the result of that is what you can see down here.

307
00:13:37,360 --> 00:13:40,830
‫Okay, let's put it back

308
00:13:40,830 --> 00:13:41,900
‫now it works.

309
00:13:41,900 --> 00:13:43,920
‫Now let's go into the file,

310
00:13:43,920 --> 00:13:45,880
‫create a dog that doesn't exist

311
00:13:50,010 --> 00:13:54,320
‫and now, oh, here we have this big weird error

312
00:13:54,320 --> 00:13:57,583
‫and that's because we're simply logging the entire error

313
00:13:57,583 --> 00:14:00,700
‫and not the error message as we were doing before

314
00:14:00,700 --> 00:14:01,723
‫but doesn't matter.

315
00:14:03,500 --> 00:14:05,203
‫Now give it a save here now,

316
00:14:06,170 --> 00:14:07,220
‫put it back

317
00:14:07,220 --> 00:14:08,500
‫and all right.

318
00:14:08,500 --> 00:14:10,210
‫So now it's all working

319
00:14:10,210 --> 00:14:12,510
‫and we got rid of that triangular shape

320
00:14:12,510 --> 00:14:14,040
‫that we had in our code

321
00:14:14,040 --> 00:14:18,420
‫and now have a so called flat structure of chain promises.

322
00:14:18,420 --> 00:14:20,600
‫So again this is way easier to manage

323
00:14:20,600 --> 00:14:23,150
‫and I hope you can start to appreciate that

324
00:14:23,150 --> 00:14:25,430
‫and it's also more logical to think

325
00:14:25,430 --> 00:14:28,120
‫and to reason about the code like this.

326
00:14:28,120 --> 00:14:29,270
‫Now, in the next lecture

327
00:14:29,270 --> 00:14:31,660
‫we will actually take it one step further even

328
00:14:31,660 --> 00:14:33,570
‫and make this even more readable

329
00:14:33,570 --> 00:14:35,210
‫and easier to use.

330
00:14:35,210 --> 00:14:36,783
‫So stay tuned for that.

