1
00:00:00,240 --> 00:00:00,760
Beautiful.

2
00:00:00,780 --> 00:00:06,780
And once we have looked at the Synchronoss approach like promised, I also would want to show you the

3
00:00:06,810 --> 00:00:11,700
asynchronous and as always, let's start by creating a new file can.

4
00:00:11,700 --> 00:00:18,690
In my case, that is going to be 10 hyphen and then I'm going to go with F.A.A. Sync Andrius.

5
00:00:19,020 --> 00:00:24,800
I'll grab everything in the objets and then just copy and paste back in the address.

6
00:00:24,810 --> 00:00:31,500
I would want to change these names so I'm not looking anymore for the red files and I'm actually looking

7
00:00:31,500 --> 00:00:33,990
for a method by the name of red file.

8
00:00:34,320 --> 00:00:38,820
So this is going to be the asynchronous one and the same goes with right file.

9
00:00:39,120 --> 00:00:42,830
Now, of course, we can remove all of this code as well.

10
00:00:43,260 --> 00:00:48,240
And the way the synchronous method works, we need to provide a callback.

11
00:00:48,480 --> 00:00:55,590
And if you're familiar with callback, essentially what it means is that we run that callback when we

12
00:00:55,590 --> 00:00:56,030
are done.

13
00:00:56,760 --> 00:01:04,769
So when whatever functionality we're doing is complete, then we run that callback just like we normally

14
00:01:04,769 --> 00:01:07,560
would do with adventitious there on a button.

15
00:01:07,840 --> 00:01:13,230
Remember, we go with the listener, we add whatever the event, of course, with a button that would

16
00:01:13,230 --> 00:01:16,500
be a click event and then pass in the callback function.

17
00:01:16,650 --> 00:01:20,400
And that callback function will run every time we click on a button.

18
00:01:20,730 --> 00:01:25,170
So this is the same thing where we have the read file function.

19
00:01:26,150 --> 00:01:30,920
And we can simply use it, since we have the structure that we need to provide a path.

20
00:01:31,220 --> 00:01:37,030
So again, we go forward slash content and the file name is first.

21
00:01:38,120 --> 00:01:42,050
And this is where we provide that second argument.

22
00:01:42,230 --> 00:01:44,190
And that is going to be a callback.

23
00:01:44,780 --> 00:01:47,930
Now, in that callback function, we pass into more arguments.

24
00:01:47,970 --> 00:01:54,380
First one is going to be error if there is one, as well as the result online instead of the body.

25
00:01:54,590 --> 00:02:00,680
First, I would want to check if there is an error and then I can just return null as well as maybe

26
00:02:00,710 --> 00:02:01,350
console log.

27
00:02:01,640 --> 00:02:08,419
So we go with F error and then we say return now or simply return whatever you want.

28
00:02:08,630 --> 00:02:11,330
And if you want, you can log the error.

29
00:02:11,870 --> 00:02:19,520
So and then if everything is correct, then I'll have access to my data, which of course has the name,

30
00:02:19,520 --> 00:02:21,680
the parameter name of Russell.

31
00:02:21,890 --> 00:02:28,100
And since I would simply want to log the result, I'm going to go with console log and the result,

32
00:02:28,120 --> 00:02:32,440
keep in mind that this is where you would set up the functionality.

33
00:02:32,720 --> 00:02:39,950
So let's go back and we're going to go with node abcess and in this case, something really interesting

34
00:02:40,340 --> 00:02:43,480
where we don't get the text right.

35
00:02:43,640 --> 00:02:45,010
So we're getting this buffer.

36
00:02:45,350 --> 00:02:51,690
So this is what's going to happen where if we don't provide that UTF coding, we'll get the buffer.

37
00:02:51,980 --> 00:02:59,030
Now, where do we have this encoding value right here in between in between the content as well as the

38
00:02:59,030 --> 00:02:59,360
path.

39
00:02:59,660 --> 00:03:04,490
So let's just go with string and we're looking for the UTF.

40
00:03:04,820 --> 00:03:12,740
So let's say UTF eight and seven, and let's run it one more time and of course, have no Rapture's.

41
00:03:12,950 --> 00:03:16,410
And then hello, this is the first text file.

42
00:03:16,880 --> 00:03:18,410
Now, the million dollar question.

43
00:03:18,860 --> 00:03:21,110
Well, how we can set up the same functionality.

44
00:03:21,980 --> 00:03:22,550
Check it out.

45
00:03:22,880 --> 00:03:29,660
We have read file, read file and then we simply create a new file with both of these routers.

46
00:03:29,990 --> 00:03:37,100
So if you're familiar with callbacks, you know that we would need to set up that functionality inside

47
00:03:37,100 --> 00:03:39,740
of this callback where I'm getting this result.

48
00:03:40,400 --> 00:03:45,590
Otherwise, there's going to be an error, because the only way for me to access this result, if I'm

49
00:03:45,590 --> 00:03:49,310
setting this as a callback, is, of course, inside of this function.

50
00:03:49,640 --> 00:03:56,780
And this is what I was saying before, where, yes, eventually I'll show you a different approach because

51
00:03:56,990 --> 00:03:59,690
inevitably we will set up a callback.

52
00:03:59,690 --> 00:04:01,890
Hell, and you'll see that in a second.

53
00:04:02,150 --> 00:04:08,960
So what I would want to do right now, when I get back my first text file, well, I would want to assign

54
00:04:08,960 --> 00:04:12,910
it to some kind of variable and then I would want to read that second file.

55
00:04:12,920 --> 00:04:13,190
Right.

56
00:04:13,550 --> 00:04:18,769
And then eventually, once I read the second file, then, of course, I would want to create that new

57
00:04:18,769 --> 00:04:19,120
file.

58
00:04:19,550 --> 00:04:26,330
And the way it's going to look like I'm going to go with const, I'll say first and you can name it

59
00:04:26,330 --> 00:04:26,680
first.

60
00:04:27,230 --> 00:04:31,090
I'm just going to go first and this will be equal to my result.

61
00:04:31,340 --> 00:04:31,720
Awesome.

62
00:04:32,060 --> 00:04:38,440
And then right after that, the result again I'm going to go with read file and then the same thing.

63
00:04:39,020 --> 00:04:45,960
We provide a path so we go with content and then in this case we're looking for second and then the

64
00:04:46,670 --> 00:04:48,650
then we need to provide the encoding.

65
00:04:49,010 --> 00:04:51,770
So Node knows how to decode that.

66
00:04:51,770 --> 00:04:56,560
And we'll go at UTF eight and then we need to provide our callback again.

67
00:04:56,570 --> 00:04:57,380
Same deal.

68
00:04:57,380 --> 00:05:00,110
We go with error and result.

69
00:05:00,320 --> 00:05:03,100
And in order to speed this up, I will copy and paste.

70
00:05:03,320 --> 00:05:07,070
So just grab everything starting with F and ending with the result.

71
00:05:07,440 --> 00:05:09,040
Copy and paste.

72
00:05:09,230 --> 00:05:12,620
Again, this is the case where we're checking for error.

73
00:05:12,620 --> 00:05:18,530
First, if there is an error, we cancel it and if everything is successful, we assign this one to

74
00:05:18,530 --> 00:05:23,450
the second one and then eventually now we want to set up my right file.

75
00:05:23,660 --> 00:05:29,830
So again, it is a function we need to provide that value and we already have a result sync.

76
00:05:30,770 --> 00:05:35,300
That's why I'll create a new one and I'll call this result and async.

77
00:05:35,570 --> 00:05:40,940
So again, we're looking for the content and we'll go with the file line, which is going to be a result

78
00:05:40,940 --> 00:05:46,610
and async text, since there is no file node will create that for us.

79
00:05:46,970 --> 00:05:47,660
All right away.

80
00:05:47,660 --> 00:05:48,510
Set up my data.

81
00:05:48,590 --> 00:05:52,130
Now, if you want, you can copy and paste this functionality.

82
00:05:52,460 --> 00:05:55,790
This is totally up to you where basically we use two of them.

83
00:05:56,000 --> 00:06:00,410
And in my case, this is exactly what I'm going to do in order to speed this up if you want to create

84
00:06:00,410 --> 00:06:03,410
something from scratch, because that is also an option.

85
00:06:03,590 --> 00:06:05,330
And I'm just going to copy and paste.

86
00:06:05,330 --> 00:06:06,610
That's my template string.

87
00:06:06,830 --> 00:06:11,480
And again, if you want this flag, you can definitely do so.

88
00:06:11,660 --> 00:06:14,100
In my case, I'm just going home at that.

89
00:06:14,390 --> 00:06:15,440
So here's the result.

90
00:06:15,440 --> 00:06:23,270
I have first and second and the reason why I have access to them, because I'm looking in the outside

91
00:06:23,270 --> 00:06:24,740
scope of this.

92
00:06:25,820 --> 00:06:32,500
And then again, we have a callback function in the right, while I know there's tons of callback functions,

93
00:06:32,510 --> 00:06:37,760
that's why we'll have to set up a better setup when we're handling these callbacks.

94
00:06:38,030 --> 00:06:39,590
But we'll do that a little bit later.

95
00:06:39,620 --> 00:06:46,280
So for now, let's just add a comment here where we have a first or second parameter and then third

96
00:06:46,280 --> 00:06:46,830
parameter.

97
00:06:47,120 --> 00:06:54,210
This is going to be, again, our callback where we have error and result and then in-service callback.

98
00:06:54,470 --> 00:07:00,530
Finally, we can take a look at the value and of course, the value will be in this result.

99
00:07:00,560 --> 00:07:05,210
Now, of course, we're creating the file and all that, but hopefully you get the gist that if you

100
00:07:05,210 --> 00:07:09,320
would be doing something where you're looking for this result, it would be over here.

101
00:07:09,650 --> 00:07:11,930
So, again, let's check for Eric quickly.

102
00:07:12,170 --> 00:07:19,340
If there is an error that I would want to return, as well as log the error just in case.

103
00:07:19,550 --> 00:07:23,510
And if everything is correct, let me just cancel the result.

104
00:07:23,810 --> 00:07:24,830
So save it here.

105
00:07:25,280 --> 00:07:28,130
And two things we need to watch out for.

106
00:07:28,430 --> 00:07:30,530
Maybe there is an error on second one.

107
00:07:30,650 --> 00:07:35,030
We should create a new file by the result, async the day.

108
00:07:35,300 --> 00:07:39,860
Let me clear the console and we're going to run node abcess.

109
00:07:40,100 --> 00:07:48,110
And I do get an error and are simply tells me that I did not add the dot in front of the content, so

110
00:07:48,110 --> 00:07:52,250
I would need to go back to the right file and just add up.

111
00:07:52,610 --> 00:07:59,570
And once we have the dot, we are going to be in good place where I'll clear the console all on one

112
00:07:59,570 --> 00:08:03,780
more time node dress and then the result will be undefined.

113
00:08:04,130 --> 00:08:05,450
OK, that's fine.

114
00:08:05,720 --> 00:08:11,780
Like I said, in this case, we're not expecting anything back, but that's really cool that in the

115
00:08:11,780 --> 00:08:16,110
result async we should have our next year is the result.

116
00:08:16,130 --> 00:08:19,190
Hello, this is first text and then the second text.

117
00:08:19,520 --> 00:08:24,400
So hopefully it is clear this is how we would use the async pattern.

118
00:08:24,740 --> 00:08:30,310
So let me zoom out right now here and you'll see this famous callback hell.

119
00:08:30,560 --> 00:08:38,120
And in the next video, I would want to give you a brief description of what our differences between

120
00:08:38,120 --> 00:08:45,320
the asynchronous approach or non blocking approach and the blocking on the synchronous one, as well

121
00:08:45,320 --> 00:08:49,220
as what are the alternatives to the callback.

