1
00:00:00,180 --> 00:00:06,360
All right, and once we have covered why it's so important to use the asynchronous approach now, of

2
00:00:06,360 --> 00:00:10,070
course, let's clean up our F a sink.

3
00:00:10,530 --> 00:00:15,530
So instead of using these nested callbacks, let's see a nice pattern.

4
00:00:15,840 --> 00:00:22,470
And as a side note, if you want to see the code that we wrote in a previous video and the one we were

5
00:00:22,470 --> 00:00:31,140
about to write and the files are located in two async patterns folder, so I'll close everything for

6
00:00:31,140 --> 00:00:31,470
now.

7
00:00:32,439 --> 00:00:38,390
And then back in objets, I can remember, like I said, if you want to access that code, it is sending

8
00:00:38,410 --> 00:00:47,950
in the two async patterns folder and we'll simply start by recreating the read file setup where I'm

9
00:00:47,950 --> 00:00:48,950
going to go with CONST.

10
00:00:49,750 --> 00:00:55,810
I'm right away accessing that from the filesystem module and I'll set it equal to require, of course,

11
00:00:56,140 --> 00:00:57,650
on that file system.

12
00:00:57,880 --> 00:00:59,090
All right, that's awesome.

13
00:00:59,260 --> 00:01:04,830
We know that it is a asynchronous so we need to set it up with three parameters.

14
00:01:04,840 --> 00:01:10,270
We go with read file and then we would want to pass in the path first.

15
00:01:10,570 --> 00:01:16,090
So again, since I mean, I'm just going to be looking for the content online forward slash and then

16
00:01:16,090 --> 00:01:18,100
first the next the file.

17
00:01:18,130 --> 00:01:20,380
We already created before then we have encoding.

18
00:01:20,380 --> 00:01:21,070
So comma.

19
00:01:21,280 --> 00:01:26,890
And then again we're going to go with UTF eight and the last one is going to be that callback where

20
00:01:26,890 --> 00:01:32,810
the first value is the error and then the second one is the data that we're getting back.

21
00:01:33,160 --> 00:01:33,460
OK.

22
00:01:34,450 --> 00:01:41,080
So once the file system responds, then, of course we invoke this callback and instead of that callback,

23
00:01:41,290 --> 00:01:42,670
we have our logic.

24
00:01:42,910 --> 00:01:48,640
So if there's going to be an error for the time being, I will just return.

25
00:01:48,780 --> 00:01:53,680
OK, so, um, then if I have the data, I'll set it up else.

26
00:01:54,070 --> 00:01:57,220
So say here comes a log on there.

27
00:01:57,220 --> 00:01:57,730
Of course.

28
00:01:58,090 --> 00:02:01,460
And we shouldn't be surprised if we see in a console.

29
00:02:01,510 --> 00:02:01,810
Hello.

30
00:02:01,810 --> 00:02:05,710
This is a first text file because we already covered the setup.

31
00:02:05,710 --> 00:02:06,050
Right.

32
00:02:06,340 --> 00:02:11,830
So we're passing the path we pass in the encoding and then we set up the callback.

33
00:02:12,340 --> 00:02:13,450
Now the problem starts.

34
00:02:13,780 --> 00:02:20,320
If we would want to perform multiple actions, if I want to read two files and then, for example,

35
00:02:20,530 --> 00:02:21,220
write one.

36
00:02:21,640 --> 00:02:22,120
Correct.

37
00:02:22,330 --> 00:02:24,400
So what would be a better solution?

38
00:02:24,430 --> 00:02:31,720
Well, a better solution would be turning this into a promise and then eventually we'll set up a sync

39
00:02:31,720 --> 00:02:32,120
away.

40
00:02:32,380 --> 00:02:37,570
Now, please keep in mind that the only reason why we'll type out the promise stuff is because I would

41
00:02:37,570 --> 00:02:41,050
want you to understand what is happening behind the scenes.

42
00:02:41,420 --> 00:02:46,870
But don't worry, throughout the course, we'll just stick with async and avoid syntax.

43
00:02:47,140 --> 00:02:51,660
And essentially, you won't have to worry about what is happening behind the scenes.

44
00:02:51,670 --> 00:02:56,430
But the first example, yes, I want you to understand everything in detail.

45
00:02:56,860 --> 00:02:59,350
So what we could do is create a new function.

46
00:03:00,180 --> 00:03:07,770
And we can just say Consed, and then I'll call this get text now this text will take a bath since I

47
00:03:07,890 --> 00:03:12,900
want to read two files and then eventually write one as well, just like we did here.

48
00:03:13,110 --> 00:03:18,710
And of course the goal here is to showcase how we can make the code in the face a sink.

49
00:03:18,720 --> 00:03:23,100
At least that's how I called my file much cleaner and more readable.

50
00:03:23,340 --> 00:03:26,010
So I'm going to pass in the path I'm on.

51
00:03:26,010 --> 00:03:30,290
One I would want to return from this function is a promise.

52
00:03:30,480 --> 00:03:38,880
So we go return new promise and that in the promise object, what we need to pass in is another callback

53
00:03:38,880 --> 00:03:39,360
function.

54
00:03:39,370 --> 00:03:46,200
So we pass and the callback function and in here we pass in two more functions, one for resolve and

55
00:03:46,200 --> 00:03:47,820
then one for project.

56
00:03:47,970 --> 00:03:50,410
And then once I have set up both of these parameters.

57
00:03:50,440 --> 00:03:58,830
Now, of course, what I want is to move the read file and place it inside of the promised one.

58
00:03:59,390 --> 00:04:04,110
OK, so once we place it here, first of all, notice here how I'm passing in the path.

59
00:04:04,440 --> 00:04:08,900
So of course I can change this one around so I can say path I'm on.

60
00:04:08,940 --> 00:04:13,920
The encoding stays the same as well as the callback for inside of the callback.

61
00:04:14,220 --> 00:04:21,240
Once I have access to resolve and reject what I could simply say that if there is an error, I will

62
00:04:21,240 --> 00:04:22,610
spit back the error.

63
00:04:22,770 --> 00:04:25,050
So say reject an error.

64
00:04:25,050 --> 00:04:31,440
And if everything goes great now, of course, let's go with a resolve and mine will pass in the data.

65
00:04:31,740 --> 00:04:38,640
And once we have the setup, what's really cool that now I can invoke get text and this is going to

66
00:04:38,640 --> 00:04:46,420
return a promise and then of course we can change that then and catch so below the text I can go and

67
00:04:46,440 --> 00:04:47,160
get text.

68
00:04:47,460 --> 00:04:52,140
And of course we're looking for the path, so we still would need to use that and then forward slash

69
00:04:52,140 --> 00:04:55,520
and then content, of course, and first the act.

70
00:04:55,860 --> 00:04:56,820
So that's the path.

71
00:04:57,060 --> 00:05:02,730
And then like I said, we are returning a promise so we can change that then.

72
00:05:03,090 --> 00:05:09,600
And of course here I can have the result and I'll just cancel it for now or assault.

73
00:05:09,840 --> 00:05:13,590
And then I can also change that sketch here.

74
00:05:13,590 --> 00:05:15,230
I'm passing in the error.

75
00:05:15,330 --> 00:05:19,230
So again, we console Target and we simply would want to see the error.

76
00:05:19,590 --> 00:05:20,370
I save it.

77
00:05:20,670 --> 00:05:21,540
And beautiful.

78
00:05:22,360 --> 00:05:28,660
Again, in Arkansas, I have hello, this is first text file, so I know that it worked and also if

79
00:05:28,660 --> 00:05:36,310
I change the path, so if I make my path incorrect, we should see in the console the error where,

80
00:05:36,310 --> 00:05:38,160
of course, node cannot find the file.

81
00:05:38,560 --> 00:05:44,350
So if that works now, of course, we can take a look at how we can make it even cleaner by setting

82
00:05:44,350 --> 00:05:45,960
up a sink or.

