WEBVTT 00:05.900 --> 00:12.050 In this video you're going to carry on with the FSA module doing something similar to the previous video. 00:12.380 --> 00:16.540 But this time you're going to go for the asynchronous way. 00:16.610 --> 00:24.040 So this means that you're gonna be using callback functions in your FSA operations so javascript code. 00:24.080 --> 00:26.680 In this case is going to be unblocking. 00:26.690 --> 00:33.470 So this means that JavaScript can move to the next line while an FSA operation is waiting for a certain 00:33.470 --> 00:35.450 callback to be executed. 00:36.260 --> 00:39.860 So following is the structure of our Electra. 00:39.860 --> 00:45.540 So first of all we're going to look at how to check if a file exists using the asynchronous way. 00:45.560 --> 00:51.690 There are a couple of ways we can do this and I'm gonna explain the difference between those two. 00:51.980 --> 00:59.030 And then after that you're going to play with the read write and append operations using the asynchronous 00:59.030 --> 00:59.690 way. 00:59.720 --> 01:04.110 So let's start with checking if a certain file exists. 01:04.220 --> 01:09.970 So let's check if Mars dot text exists. 01:10.070 --> 01:14.750 So I'm going to copy some code from the previous file 01:23.460 --> 01:26.930 where we required the FS module 01:32.280 --> 01:36.450 and then defined variables where we stored the names of our files. 01:36.480 --> 01:41.130 So first let's let's check if Mars exists. 01:41.130 --> 01:46.290 So to do this you can't use the method starts. 01:46.410 --> 01:52.600 So this method is to be used if you're planning to manipulate the file afterwards. 01:53.130 --> 02:01.830 So basically if you are planning to read a file or write into the file and you just want to make sure 02:01.860 --> 02:05.520 that it does exist then you can use this method by the way. 02:05.520 --> 02:11.060 There is another method called exists which is deprecated so you can use this one he said. 02:11.070 --> 02:19.970 So this method will take the name of your file or the path of the file and it will take a function with 02:19.970 --> 02:20.150 you. 02:20.310 --> 02:21.800 She's gonna be a callback. 02:21.810 --> 02:32.180 Sarah is give us a list call the private server error for error and basically what we're gonna do we'll 02:32.190 --> 02:40.690 check if the error exists and if that's the case we'll just log the error. 02:41.030 --> 02:43.970 Okay so let's try this 02:56.240 --> 02:59.590 okay so we're not getting any error. 02:59.650 --> 03:04.630 This means that the file exists so we can just write. 03:04.690 --> 03:14.130 Else console log was exists 03:20.110 --> 03:23.020 so it's trying it. 03:23.240 --> 03:23.840 Here we go. 03:23.900 --> 03:26.210 Master text exists. 03:26.750 --> 03:31.340 Okay so the next method is to be used. 03:33.000 --> 03:43.080 2 well check user permissions and also you can use it if you are most planning to manipulate the file 03:43.320 --> 03:43.800 later on. 03:44.310 --> 03:54.650 So for example if you are trying trying to read a file then it's not recommended to use the method which 03:54.650 --> 04:03.350 is called FS as does access but instead use you use straight away the read method which we will cover 04:03.410 --> 04:09.130 in a bit and you just manipulate errors using the callback okay. 04:09.450 --> 04:16.170 So access is usually used to check the user permissions of the file and will see in a bit how to do 04:16.170 --> 04:16.450 that. 04:17.430 --> 04:25.170 So this method will take the path of the file as well. 04:27.250 --> 04:30.010 And a callback 04:38.720 --> 04:45.450 So for example we can just console that log something like error 04:48.230 --> 04:48.740 then error 04:56.680 --> 05:07.710 so let's save and try this okay so error here gives us no because there is no error. 05:07.900 --> 05:12.700 So instead of writing it s like this you can check if the error exists. 05:14.550 --> 05:18.750 Like that and then log it if that's the case 05:23.880 --> 05:24.990 okay. 05:25.110 --> 05:31.660 Now if you try a file that does not exist then you will get an error. 05:35.120 --> 05:37.580 So errors here. 05:37.580 --> 05:39.470 They have an error code. 05:39.560 --> 05:48.400 So in this example the error code is anyones which refers to a file that does not exist. 05:48.470 --> 05:52.430 If you want to look up the list of error codes you can go to the. 05:52.460 --> 05:56.150 No documentation and then look for errors 05:58.810 --> 06:03.340 and here at the bottom if you type error bots code 06:06.600 --> 06:13.020 you'll find this link if you click on it then it will give you the different error codes. 06:14.690 --> 06:16.550 Thus you may encounter. 06:16.590 --> 06:18.360 If you search common 06:21.320 --> 06:22.250 system errors 06:25.410 --> 06:33.510 you will find a common list of system errors and one of them is anyone's. 06:33.630 --> 06:38.340 And this refers to no such file or directory. 06:38.450 --> 06:40.440 Okay so the common ones are right there. 06:40.440 --> 06:41.370 So for example 06:45.130 --> 06:57.860 if you get an error saying in 0 0 to empty then it s it s means that the directory is n empty okay. 06:57.900 --> 07:05.900 So that's something you get if you try in an operation that requires ie an empty directory for example. 07:06.600 --> 07:12.100 Okay so let's go back to our file. 07:12.140 --> 07:16.580 Let's go back to Mars like that. 07:16.810 --> 07:26.000 Alright so this function access normally takes a second parameter which is a constant. 07:27.080 --> 07:37.340 So by default this constant is set to FS dot constant dot f underscore. 07:37.370 --> 07:46.800 Okay so this is the default constants that you would use to check if a file exists now to check the 07:46.860 --> 07:54.730 list of these constants you can just type F as those constants in Google 08:02.170 --> 08:04.360 let's go through the documentation page 08:10.820 --> 08:17.180 if you click the you'll find the file access constants. 08:17.270 --> 08:25.700 So these are the constants does you would use with the access method of FS so the syntax is F is those 08:25.700 --> 08:29.450 constants dot the constant you want. 08:29.450 --> 08:35.330 So this is the default one which tells us that the file exists. 08:35.330 --> 08:41.410 Now if you want to check if a file is readable then it's R and it's core okay. 08:41.690 --> 08:42.600 If it's right I will. 08:42.660 --> 08:51.350 S W instead of R and if it's executable then it's X and it means as it it's exec it can be executed 08:51.410 --> 08:53.180 by the calling process. 08:53.720 --> 09:02.740 Okay so now let's try with F and the score okay and see what we get. 09:02.780 --> 09:06.690 So or instead R the score. 09:06.730 --> 09:08.590 Okay sorry. 09:15.820 --> 09:28.170 We're not getting any error because our file is readable if you try with W again each would work because 09:28.170 --> 09:30.140 the file is a all. 09:30.570 --> 09:40.690 Now what if we change the permissions of the file before trying that's so let's say that we want to 09:40.690 --> 09:41.260 change 09:47.320 --> 09:49.140 the provisions of Mars. 09:49.210 --> 10:01.450 Those text 2 4 4 4 for example meaning that the file can be read but you can't write into it so you 10:01.450 --> 10:08.840 need to add a zero at the front which says that we using an Oxford number. 10:09.130 --> 10:20.710 So the syntax is f is that change mode now file is was and 10:23.670 --> 10:28.200 we need to o permissions 10:30.900 --> 10:32.760 and a callback. 10:32.760 --> 10:35.760 So let's use the same callback you call that 10:47.470 --> 10:47.930 Australia's 10:50.710 --> 10:51.060 OK. 10:51.100 --> 10:59.010 So we're getting an error here and it says e access meaning permission denied. 10:59.020 --> 11:06.160 So basically we can't right into this file because we said the permissions to 444 meaning that we can't 11:06.160 --> 11:07.060 read the file early. 11:07.690 --> 11:10.150 So now let's change the permissions to 11:13.350 --> 11:15.480 so 7 7 7 for example. 11:15.660 --> 11:19.170 So we're giving all permissions to all user types. 11:20.350 --> 11:23.250 And this time we're not getting any error. 11:23.610 --> 11:24.820 Perfect. 11:24.810 --> 11:32.790 So hopefully this gave you an idea about these different constants you use with the access method and 11:32.790 --> 11:41.560 also how to change the mode or the permissions using the change mode method of FS okay. 11:41.760 --> 11:42.360 So 11:45.990 --> 11:51.970 now let's move to read write and append and do this the asynchronous way. 11:52.860 --> 11:58.610 So to start let's write into our file Jupiter. 11:58.710 --> 12:07.830 So the method is right file instead of right while Sink which we used with the synchronous way it will 12:07.830 --> 12:10.170 take the name of the file. 12:10.200 --> 12:22.730 So in our case Jupiter and let's add something like I am learning note so you need a callback and let's 12:22.730 --> 12:24.440 copy this callback 12:30.110 --> 12:30.460 Okay 12:36.170 --> 12:42.560 serious try this and see how it goes. 12:42.830 --> 12:46.790 You can see those we have just written into Jupiter. 12:46.790 --> 12:53.360 I am learning node so it has overwrite it whatever was inside our file. 12:53.970 --> 12:54.960 Okay perfect. 12:54.960 --> 12:59.700 So now if we have no errors we're going to have an else statement 13:04.710 --> 13:06.330 inside these statements. 13:06.360 --> 13:10.710 We aren't going to read our file Jupiter. 13:10.710 --> 13:16.090 So F as those read file instead of read file sync. 13:16.090 --> 13:21.630 So the name is Jupiter and we're using the UTF 8 encoding 13:24.710 --> 13:25.590 okay. 13:25.850 --> 13:32.240 And since we are reading inside the file now our callback is going to have to promises the error and 13:32.330 --> 13:34.120 the data returned. 13:34.310 --> 13:40.790 If we read successfully so let's copy this callback and change it slightly 13:53.100 --> 13:57.790 so instead of error we're going to have error and data. 13:57.790 --> 14:01.090 So the second problem is a is data error comes first. 14:03.360 --> 14:11.910 So if there are no errors then we're going to log the contents of our file and let's say something like 14:16.370 --> 14:19.370 contents of Jupiter is data 14:34.050 --> 14:36.530 let's have a library. 14:37.760 --> 14:45.240 OK so let's try this and we can see the contents of our file there. 14:45.600 --> 14:46.300 All right. 14:46.310 --> 14:59.330 Perfect Now let's move to the next step which is to append some other content to Jupiter so 15:02.510 --> 15:04.280 inside our else statement 15:07.890 --> 15:13.700 it's odd another statement to append content to Jupiter. 15:13.730 --> 15:20.570 So as we do this we're going to use the append file method instead of append file sync so it takes the 15:20.630 --> 15:22.930 name of the file and the contents. 15:23.420 --> 15:30.430 So let's say that we're going to have a line break and then it is so fun 15:33.630 --> 15:35.580 and let's have a callback 15:45.800 --> 15:46.100 OK 15:54.170 --> 16:03.110 then if everything goes fine and we have no errors we're going to read the file again 16:08.530 --> 16:17.300 using FS does read file which takes the name of the file the encoding 16:21.230 --> 16:23.020 we're going gonna have a callback again. 16:25.220 --> 16:31.710 So instead of error we're going to have error and data. 16:31.960 --> 16:36.850 So if there are no errors then we're going to log the data again. 16:39.310 --> 16:40.110 I'm going to copy 16:42.880 --> 16:45.880 this statement like that's 16:50.490 --> 16:50.800 OK. 16:50.800 --> 16:52.520 Perfect. 16:52.990 --> 16:57.500 So let's try this. 16:57.710 --> 16:57.950 OK. 16:57.980 --> 17:02.740 So that's the new contents of our file after appending a new line. 17:02.750 --> 17:03.760 It is so fun. 17:04.820 --> 17:07.980 And you can take that by clicking on the file. 17:08.030 --> 17:08.930 Perfect. 17:08.930 --> 17:17.150 Now if you check your code you will see that you've got a pyramid shape which you created using the 17:17.150 --> 17:18.750 callback functions. 17:18.830 --> 17:26.210 Basically you had to write a statement and every time you had a callback and then and else statements 17:26.750 --> 17:33.890 and insiders you had another statement with a callback and as a consequence this created this pyramid 17:33.890 --> 17:38.400 shape basically this shape is known as the callback hell. 17:38.540 --> 17:45.830 And the reason why it's called like us is because it can be the source of so many mistakes which will 17:47.140 --> 17:52.760 lead to a lot of debugging which will waste you too much time. 17:53.020 --> 18:01.060 So we really need to get rid of this and you can do that by using promises or EHR sevens async await. 18:01.120 --> 18:04.730 And this is something we're going to be covering in the next couple of videos. 18:04.780 --> 18:10.690 Now before we move there let's just write a statement at the end of our code to check if our code is 18:10.690 --> 18:12.250 really asynchronous. 18:12.280 --> 18:21.440 So if you're on this let's see where you get well basically at the very beginning of code we get this 18:21.500 --> 18:26.210 statement executed first and then we get the remaining of our code. 18:26.300 --> 18:34.100 And the reason why is because this statement doesn't wait for callbacks to be executed because we doing 18:34.100 --> 18:36.120 things the asynchronous way. 18:36.140 --> 18:47.810 So while javascript is waiting for files to be read written into and so on this statements can run as 18:47.810 --> 18:55.130 our code is asynchronous and this is very different from what we had in the previous example where this 18:55.190 --> 19:03.360 line of code had to wait for all this code to be executed before you can see the hello blog. 19:03.620 --> 19:09.800 Okay so in the next video you're going to build this code again using promises.