1
00:00:00,120 --> 00:00:07,140
All right, and once we have looked at the event, in theory to hammer this home, but also take a look

2
00:00:07,140 --> 00:00:15,960
at some code examples where we can see events in action as a side note, all code examples are located

3
00:00:15,960 --> 00:00:18,310
in the event, a little example directory.

4
00:00:18,690 --> 00:00:23,250
So if you need to take a look, just grab the repo and you can find it there.

5
00:00:23,670 --> 00:00:32,600
And you should be familiar with our first example since it's a async version of read file method.

6
00:00:33,060 --> 00:00:38,750
So we import Redvale from the FS module and then we have a control log.

7
00:00:38,910 --> 00:00:47,340
Started our task first, then we have read file method where we pass the path, we pass the encoding

8
00:00:47,550 --> 00:00:53,730
and then of course we have the callback and then in the callback I counsel logged first result and then

9
00:00:53,730 --> 00:00:58,770
of course we have completed the first task and then right after the read file.

10
00:00:59,720 --> 00:01:08,630
I have starting next up and something real interesting in so I can see that we first started the first

11
00:01:08,630 --> 00:01:12,680
task, then I right away have starting the next task.

12
00:01:12,890 --> 00:01:18,460
And then, of course, once I'm done, once I get back my result, then of course I have.

13
00:01:18,470 --> 00:01:23,450
Hello, this is first text file and of course, completed first task.

14
00:01:23,870 --> 00:01:31,520
And again, the reason why is this happening because read file is a synchronous and really know that

15
00:01:31,880 --> 00:01:35,690
event will offload this miscarries to a file system.

16
00:01:36,230 --> 00:01:43,340
So we start reading the file notice like, OK, we're on this line of code, then offload this one.

17
00:01:43,670 --> 00:01:48,700
And only when I get back there's the result then around the callback.

18
00:01:48,710 --> 00:01:55,850
So when we file system responds with error or the data, then we invoke this one.

19
00:01:56,060 --> 00:01:56,420
Right.

20
00:01:56,420 --> 00:01:59,480
So we offload this task and we just keep on reading the code.

21
00:01:59,720 --> 00:02:03,860
That's why we have started the first task starting the next one.

22
00:02:03,870 --> 00:02:05,510
So right away I go to the next task.

23
00:02:05,840 --> 00:02:09,650
Unless one of these asynchronous one, well, we're just offloading.

24
00:02:10,190 --> 00:02:16,760
And then once the response comes back, whether it's an hour, whether it's a success, only then we

25
00:02:16,760 --> 00:02:18,110
invoke the callback.

26
00:02:18,110 --> 00:02:21,620
Hopefully that is clear now as far as this comment.

27
00:02:22,570 --> 00:02:28,600
I only added this one because we need to keep in mind that, of course, I'm just grabbing this code

28
00:02:28,600 --> 00:02:33,550
from the file, but the file is sitting in the folder.

29
00:02:33,940 --> 00:02:40,660
So if you were to run Gnomon and then directly the file name, which is, of course, going to be in

30
00:02:40,660 --> 00:02:42,130
the loop, the path.

31
00:02:42,280 --> 00:02:47,500
No-Match, So this path only matches because of course I have app jayesh.

32
00:02:47,800 --> 00:02:53,740
But if you'll try to run this code directly in this file, that's why it's not going to match and you

33
00:02:53,740 --> 00:02:55,660
need to go one level up.

34
00:02:55,960 --> 00:03:02,970
Now, my assumption is that you're running it the same as me and you're running that with NPM Start.

35
00:03:03,280 --> 00:03:08,040
That's why I kept the path matching to the one in the US.

36
00:03:08,500 --> 00:03:09,930
Now our next example.

37
00:03:09,940 --> 00:03:17,320
So let me clear my objets close the read file as well as packages on and then the next one is a timeout.

38
00:03:17,590 --> 00:03:26,590
So select here everything and copy and paste in my address restart and then I have to console logs and

39
00:03:26,590 --> 00:03:29,080
the third one is E and set them up.

40
00:03:29,230 --> 00:03:33,580
And this is pretty typical example for JavaScript Loop as well.

41
00:03:34,030 --> 00:03:39,250
If you have taken JavaScript courses, which again I assume you have, since you're watching the node

42
00:03:39,250 --> 00:03:43,450
one, you probably remember this example where the gotcha here is following.

43
00:03:43,450 --> 00:03:48,640
Yeah, I have the console log first, then I have the second and third.

44
00:03:49,150 --> 00:03:52,930
And you think that since set time out is set to zero.

45
00:03:53,530 --> 00:03:59,950
So since we're saying you call this callback function but actually wait only zero seconds, you think

46
00:04:00,190 --> 00:04:04,530
that the order would be first, second and third, right?

47
00:04:04,900 --> 00:04:08,800
Well, wrong, because we have first, third and second.

48
00:04:09,220 --> 00:04:09,970
Why one?

49
00:04:10,030 --> 00:04:12,220
Because the timeout is asynchronous.

50
00:04:12,550 --> 00:04:12,990
Correct.

51
00:04:13,420 --> 00:04:17,000
And what happens with synchronous while they get offloaded.

52
00:04:17,440 --> 00:04:25,260
So in this case, we run first, third and then the second because this one gets offloaded again.

53
00:04:25,330 --> 00:04:27,220
This one goes to the back of the line.

54
00:04:27,490 --> 00:04:34,390
And only when we're done with our immediate code, pretty much with our asynchronous code, only then

55
00:04:34,810 --> 00:04:36,900
we invoke that callback.

56
00:04:37,360 --> 00:04:37,830
All right.

57
00:04:38,080 --> 00:04:45,340
Now, I also added of these comments here where we have started operating system process and then completed

58
00:04:45,340 --> 00:04:47,980
and exited the operating system process.

59
00:04:48,370 --> 00:04:53,980
And the reason why I'm showing you that, because in the next two examples, we'll do something a little

60
00:04:53,980 --> 00:04:54,530
bit different.

61
00:04:54,850 --> 00:04:57,760
So now I would want to go to set Intervale.

62
00:04:58,750 --> 00:05:05,260
And by the way, it's going to be a bit clearer if we around here node and then abcess, so let me stop

63
00:05:05,260 --> 00:05:12,280
this and I'll go with Noad and Objets and you'll notice that we started the operating system process.

64
00:05:12,610 --> 00:05:15,390
And then the moment we're done with the code, that's it.

65
00:05:15,430 --> 00:05:16,120
We exit.

66
00:05:16,450 --> 00:05:23,830
Now, if we'll take a look at the set interval, you'll notice something really interesting where if

67
00:05:23,830 --> 00:05:29,100
I go with Node and I notice something interesting and by the way, of course, I didn't copy and paste.

68
00:05:29,410 --> 00:05:35,780
So go to the third example and then just remove all this code and copy and paste.

69
00:05:35,800 --> 00:05:37,120
So that's my third example.

70
00:05:37,300 --> 00:05:40,500
And in here I have the set interval now again.

71
00:05:40,540 --> 00:05:44,830
Yes, we'll have to run a few times, note address and now check it out.

72
00:05:45,340 --> 00:05:49,030
So what I see here is I will run first.

73
00:05:49,510 --> 00:05:50,940
So that's my console log.

74
00:05:51,550 --> 00:05:56,160
I'm going to have the set interval and notice how we're not exiting the process here.

75
00:05:56,950 --> 00:06:00,820
So we start a process similarly to the second example.

76
00:06:01,220 --> 00:06:08,650
But if in a second example, we actually exited because we completed our task in this case, we're not

77
00:06:08,650 --> 00:06:12,690
doing that y well, because set interval is asynchronous.

78
00:06:13,540 --> 00:06:20,080
Now, the difference between the set time up and set interval is the fact that set interval runs in

79
00:06:20,080 --> 00:06:20,830
those increments.

80
00:06:21,160 --> 00:06:24,360
In this case, of course, it is those two seconds.

81
00:06:24,730 --> 00:06:31,600
So every two seconds the event loop is just going to invoke that callback.

82
00:06:31,860 --> 00:06:34,020
And that's why we're not exiting.

83
00:06:34,600 --> 00:06:36,820
That's why we're still in the process.

84
00:06:36,820 --> 00:06:40,040
And we can only exit the process if we kill it.

85
00:06:40,360 --> 00:06:48,160
So that would be control and C or there's some unexpected error, otherwise it will always stay alive.

86
00:06:48,280 --> 00:06:55,090
Now, again, keep in mind one thing where notice I will run first was first.

87
00:06:55,090 --> 00:06:55,440
Why?

88
00:06:55,720 --> 00:07:00,610
And because, again, this is a synchronous and I know I've said this before twenty thousand times.

89
00:07:00,610 --> 00:07:07,600
But again, this is probably the core building block of know the fact that every time we have some synchronous

90
00:07:07,600 --> 00:07:09,570
action, it's just going to be offloaded.

91
00:07:09,580 --> 00:07:16,210
And then when it's time we invoke the callback and our last example is a server.

92
00:07:16,210 --> 00:07:21,610
And again, I just wanted to showcase how the process stays alive.

93
00:07:21,970 --> 00:07:30,010
So I'll take all the code and copy and paste in jazz and I'll stop this one.

94
00:07:30,010 --> 00:07:33,640
So stop the process and then we'll clear the console.

95
00:07:33,640 --> 00:07:39,820
And again, just to showcase how the process stays alive, I'm going to go with a node and abcess and

96
00:07:39,820 --> 00:07:40,390
I'm doing that.

97
00:07:40,400 --> 00:07:44,260
So you're not confused with Nortman and then check it out.

98
00:07:44,470 --> 00:07:54,820
So we have a server listening on Port 5000 and every time the request comes in, well, we invoke this

99
00:07:54,820 --> 00:08:01,630
callback and in our callback we're simply Consolmagno request event and then we send back the hello

100
00:08:01,630 --> 00:08:01,940
world.

101
00:08:02,200 --> 00:08:07,750
So now if I were to go to a local host, 5000, there it is.

102
00:08:08,470 --> 00:08:11,890
I have the response of our world in a console.

103
00:08:12,100 --> 00:08:18,040
I'll see this request event and check it out how again, this process stays alive.

104
00:08:18,460 --> 00:08:18,860
Why?

105
00:08:18,910 --> 00:08:21,730
Well, because listen is asynchronous.

106
00:08:22,450 --> 00:08:30,560
And the moment we set it up now, event loop is just waiting for those requests to come in and then

107
00:08:30,610 --> 00:08:31,480
once they come in.

108
00:08:31,690 --> 00:08:34,419
But of course, we run our callback.

109
00:08:34,750 --> 00:08:41,250
Now, please don't confuse this callback with what we're responding our request to one.

110
00:08:41,559 --> 00:08:47,500
So this callback is just when we're setting up the server, because in here we can have the success

111
00:08:47,500 --> 00:08:48,210
over error.

112
00:08:48,430 --> 00:08:50,710
So in our case, as you can see, everything went great.

113
00:08:50,950 --> 00:08:57,070
We have a server listening on Port 5000, but of course, there also might be an error.

114
00:08:57,460 --> 00:09:01,990
And this one, of course, just responds to that request event.

115
00:09:02,200 --> 00:09:09,460
But the whole point is we have server that listen, we just say, hey, Evenflo, just keep listening

116
00:09:09,790 --> 00:09:16,510
for those incoming requests and the moment show up line, respond to them appropriately.

117
00:09:16,750 --> 00:09:17,950
Hopefully it is clear.

118
00:09:18,160 --> 00:09:20,860
And of course, we can move on to our next topic.

