WEBVTT 00:05.620 --> 00:11.920 In this video you're going to improve code you wrote in the previous video and get rid of the pyramid 00:11.950 --> 00:13.420 shape in your code. 00:13.420 --> 00:16.140 Or what is called as the callback hell. 00:16.840 --> 00:20.310 So to do this you're going to use promises. 00:20.590 --> 00:28.000 So the FSD module comes with a nice API called FSI promises. 00:28.000 --> 00:32.100 Now just bear in mind that this API is still unstable I. 00:32.140 --> 00:35.640 One of the moments meaning that it is experimental. 00:36.010 --> 00:43.480 So as we're talking now it is experimental but hopefully very shortly it should be stable. 00:43.960 --> 00:50.890 So most features inside these API were added in Version 10. 00:50.910 --> 00:55.880 Now since we are running version eleven we should be OK. 00:56.320 --> 01:02.420 So to use this you really need to be using version 10 or beyond that. 01:02.530 --> 01:03.550 All right. 01:03.550 --> 01:13.460 So let's go to our code and transform whatever we have written using callbacks to promises. 01:13.480 --> 01:21.190 So first of all I'm going to summarize what ever we have written inside this code using some simple 01:21.190 --> 01:22.020 comments. 01:22.030 --> 01:27.280 So first of all we have written inside. 01:27.460 --> 01:37.510 I'll file Jupiter using Wright's file and then we read the contents of our file Jupiter using read file 01:40.670 --> 01:43.880 and then we looked the contents into the console 01:50.310 --> 01:51.310 okay. 01:51.390 --> 01:56.160 And then after that we appended some contents to our file using a pen file 01:59.750 --> 02:04.070 and after that we read the contents of our file again 02:10.490 --> 02:19.720 and we logged that contents to console. 02:20.230 --> 02:29.240 And every time we run any of those operations we had to write a callback which takes care of the error 02:30.110 --> 02:35.450 and also introduced and else statements to carry on if everything goes fine. 02:36.230 --> 02:42.500 So if you rise promises then you're gonna catch the error right at the end. 02:42.500 --> 02:50.540 So if this is your promises code then right at the end you're going to have a catch statement where 02:50.660 --> 02:58.880 you're going to catch any error that's made or that might occur in your code. 02:58.980 --> 03:03.840 So let's go ahead and write our code using promises. 03:03.840 --> 03:16.020 So to use FSA promises you need to go to the FS objects which you said earlier to require efforts and 03:17.070 --> 03:19.020 go to promises. 03:19.020 --> 03:25.190 So this objects comes with many methods which are very similar to the ones you use directly with efforts. 03:25.830 --> 03:36.610 So to writes inside a file or override the contents of the file you still use write file instead of 03:36.790 --> 03:37.420 right file. 03:37.420 --> 03:42.570 At best it's going to be a right file of FS not promises. 03:42.970 --> 03:51.730 Okay so the syntax is very similar to the one you used before. 03:52.210 --> 03:57.760 Why instead of having a callback This is returning a promise. 03:57.780 --> 04:06.360 Okay so you can use a then statements straightaway. 04:06.380 --> 04:14.600 Now just make sure as you are comfortable with promises before diving into the remaining of this lecture. 04:15.080 --> 04:23.180 Otherwise you can always go back to the E6 section and review promises and then come back once you feel 04:23.180 --> 04:24.350 comfortable. 04:24.460 --> 04:24.740 Okay. 04:24.770 --> 04:34.760 So now once we have written into the file we are going to read the contents of our file. 04:35.770 --> 04:36.700 So to do this 04:39.930 --> 04:55.120 we're going to pass a function inside the method then okay and to read inside the file. 04:55.880 --> 05:00.880 We're gonna use f s those promises but read file. 05:00.950 --> 05:09.710 Now we're going to return the contents must be read inside the file so we don't need curly brackets. 05:09.740 --> 05:09.980 Okay. 05:10.010 --> 05:17.960 So if you use the S6 way then what's ever gonna write here is going to be returned by this function 05:18.740 --> 05:30.230 so f FS those promises that read file and we pass to this method these same promises we passed earlier 05:30.290 --> 05:32.400 without the callback. 05:32.890 --> 05:33.140 Okay. 05:33.140 --> 05:36.320 So this is going to return a promise. 05:36.320 --> 05:44.970 So let's have another then statement. 05:45.080 --> 05:57.140 Now once we read inside the file we're going to have some content which is the value of the data objects 05:57.140 --> 06:06.480 we had earlier so in this new function we're going to have a variable primary set data for the function 06:07.500 --> 06:15.990 and what we want to do we just want to console those log Jupiters contents data 06:22.760 --> 06:23.400 like this. 06:25.430 --> 06:26.610 Okay. 06:27.110 --> 06:31.010 Now let's move to the next step which is appending to our file 06:40.410 --> 06:42.480 so we're gonna use. 06:42.520 --> 06:45.500 Those promises dots append file. 06:45.570 --> 06:51.450 Okay so we're passing a function which doesn't take any promises and 06:56.200 --> 07:03.680 we're using F as those promises append file to it then file is going to take the same same parameters 07:03.710 --> 07:09.830 we used earlier without the callback like. 07:10.520 --> 07:11.310 Okay. 07:11.500 --> 07:17.040 So once we append into our file we're going to read the file again. 07:17.380 --> 07:21.780 So you gonna use the same statements we are there. 07:22.090 --> 07:25.570 And then once we read we want to console that log the contents. 07:25.650 --> 07:29.370 So you gonna use both those statements. 07:29.490 --> 07:30.270 Okay. 07:30.400 --> 07:35.370 So we are right there and now we just need to catch any errors. 07:37.020 --> 07:40.630 So to do that we're gonna use the catch statements. 07:43.380 --> 07:50.640 Which takes us a promise that a function or callback which we'll take care of the error. 07:50.690 --> 07:57.500 So you gonna have the error as the problem is the and we just want to log the error. 07:57.580 --> 08:02.590 We can do something very similar to this like that. 08:03.230 --> 08:03.700 And here we go 08:18.850 --> 08:19.130 okay. 08:19.170 --> 08:19.710 So 08:22.990 --> 08:26.310 that s comments this code on this just deleted 08:29.960 --> 08:32.010 so again we are using promises here. 08:32.150 --> 08:34.790 Meaning that our code is asynchronous. 08:34.790 --> 08:42.830 So you will see this line being printed first and then your code running afterwards. 08:43.080 --> 08:43.430 Okay. 08:43.440 --> 08:48.270 So let's try this before doing that. 08:48.900 --> 08:51.440 Let's just change the contents of Jupiter 08:58.750 --> 09:00.660 and then see changing. 09:01.420 --> 09:07.480 So we gonna run node of up promises. 09:08.290 --> 09:08.830 And here we go 09:12.160 --> 09:12.410 okay. 09:12.410 --> 09:18.200 So we're getting a warning here saying that F is promises API is still experimental and this is something 09:18.200 --> 09:19.420 we knew already. 09:19.430 --> 09:24.600 So first of all you see the halo log because your code is asynchronous. 09:25.730 --> 09:28.430 And then we keep going. 09:28.430 --> 09:35.030 So we were on other bits inside our code which belong to whatever we have done before 09:39.410 --> 09:39.760 okay. 09:39.830 --> 09:42.370 So this is coming from a previous code. 09:42.440 --> 09:49.440 So the contents of Jupiter at the beginning is I am learning code. 09:49.700 --> 09:50.840 So this is 09:55.720 --> 09:57.310 this line. 09:58.110 --> 09:58.570 And then 10:02.010 --> 10:09.800 the new contents after appending to our file is this one and we get the new line. 10:09.810 --> 10:11.120 It is so fun. 10:11.160 --> 10:11.630 Perfect. 10:13.840 --> 10:16.260 Our code is working perfectly now. 10:16.330 --> 10:18.740 Literally our code with an error. 10:18.740 --> 10:26.720 So let's say that we read a file that does not exist 10:34.170 --> 10:35.130 so let's try this 10:37.880 --> 10:38.130 OK. 10:38.170 --> 10:39.910 So we getting an error here. 10:40.030 --> 10:48.990 So no such file or directory and basically your code stops right there. 10:49.180 --> 10:52.900 It doesn't carry on further. 10:52.910 --> 10:55.340 So if you check your file. 10:55.340 --> 10:57.980 Jupiter here you find only the first contents. 10:57.980 --> 11:00.910 I am learning node All right. 11:01.030 --> 11:06.580 Because you couldn't or the code couldn't go any further after that error. 11:06.610 --> 11:12.970 So this is very different from what you've done before when you were using b try and catch statements. 11:12.970 --> 11:19.780 So if you had just a try and catch statements then it would have thrown an error and it would have carried 11:19.780 --> 11:21.330 on with the remaining of the gate. 11:22.600 --> 11:29.410 But here it's very different when you have a catch statements attached to your promises like Lance right 11:29.410 --> 11:38.140 on the end then it's going to cost the error and then it's going to stop right there all right. 11:38.460 --> 11:48.630 Now there is something not very good about this way of doing a catch statements on the end of promises 11:48.630 --> 11:56.780 code and that's the facts that we don't know where the error happened actually. 11:56.810 --> 12:08.160 So if you tried for example and then in existing file there instead of their 12:12.510 --> 12:17.040 then you would get the same error. 12:17.290 --> 12:21.310 And it's not very clear where the error happened in that line or in that line. 12:21.660 --> 12:29.910 So if you want to fix this you can add a rejects function instead of having only resolve functions. 12:29.910 --> 12:40.430 So what we're doing here we just passing the resolve function to the then method all the time and then 12:40.430 --> 12:42.730 we're using a catch conscious statements are the end. 12:42.770 --> 12:51.470 So instead of doing that for example when you're reading this file and then writing the then statements 12:52.010 --> 12:57.120 you can pass two functions there one for resolve and one for rejects. 12:57.200 --> 13:01.330 So let's do this so that's our first function. 13:04.250 --> 13:08.710 And then we can pass the rejects function like nuts. 13:08.850 --> 13:13.340 So let's just copy this one 13:18.610 --> 13:19.160 like this 13:25.580 --> 13:32.840 and let's just say error 1 and we can do the same thing right there 13:37.410 --> 13:49.190 and let's call this one error to so if we try this code we're supposed to get an error for this file 13:49.210 --> 13:50.200 that doesn't exist. 13:50.210 --> 14:01.540 So let's try this so we get error to now list try the correct file there and try not there. 14:01.570 --> 14:09.900 Little text right there and see what we get so we get error 1 so you can customize the error if you 14:09.900 --> 14:10.500 like. 14:10.500 --> 14:15.210 So that's one way of making sure that you know where the error happened. 14:15.210 --> 14:15.620 Okay. 14:15.750 --> 14:24.940 So for now I'm going to keep things simple and I'm going to rollback to the generic mode of catching 14:24.940 --> 14:26.710 the error right at the end 14:36.800 --> 14:44.720 so in the next video you're going to write this code again but this time you're gonna use the E sevens 14:45.140 --> 14:46.310 async await.