1
00:00:00,230 --> 00:00:07,470
All right, and just to give you an early taste of the differences between the asynchronous or non blocking

2
00:00:07,470 --> 00:00:13,710
and Synchronoss, which is a blocking code, why don't we set up a few console logs now?

3
00:00:13,710 --> 00:00:18,670
Before we do that, I will create a new file and I'll say 11 here.

4
00:00:18,690 --> 00:00:23,220
And by the way, I definitely need to zoom in just so you don't think that I'm cheating F.

5
00:00:23,220 --> 00:00:26,070
S and A think J.

6
00:00:26,190 --> 00:00:26,580
Yes.

7
00:00:27,000 --> 00:00:28,650
And let's go to the app.

8
00:00:28,730 --> 00:00:37,180
Just select everything and copy and paste in NextRadio will set up a different module in this address.

9
00:00:37,200 --> 00:00:43,300
So for the time being, I can just moment and then like I promised or set up some console along.

10
00:00:43,350 --> 00:00:45,690
So why don't we go back to FS?

11
00:00:45,690 --> 00:00:53,220
And by the way, during this video, I'll zoom out because I definitely want to go back to this evil

12
00:00:53,250 --> 00:00:59,400
evil callback hell that we're getting from this thing called back within the callback.

13
00:00:59,730 --> 00:01:01,500
And as far as if I think.

14
00:01:02,370 --> 00:01:03,280
Let me zoom in.

15
00:01:03,420 --> 00:01:11,250
Why don't we set up a council log in a few places, let's say, before we're accessing the file, so

16
00:01:11,250 --> 00:01:17,950
I'm going to go log on or say start, then we're going to go with right file sync.

17
00:01:18,240 --> 00:01:22,760
And then right after that, we're going to say I'm done with the task and then inside there.

18
00:01:22,800 --> 00:01:23,230
That's right.

19
00:01:23,310 --> 00:01:27,720
Done with this task, copy and paste.

20
00:01:28,170 --> 00:01:32,370
And we're going to go with starting the next one starting.

21
00:01:33,380 --> 00:01:42,320
The next one, meaning the next that so that should do it, I need to go with clear, so clear my counsel

22
00:01:42,560 --> 00:01:51,330
and I'm going to go to Nolde and learn the filename is 10 I found and ifs and we're looking for something.

23
00:01:51,770 --> 00:01:55,730
I run it and noticed something interesting when I have to start.

24
00:01:56,270 --> 00:02:04,640
So that is going to be before I read files and only then once I'm done writing that file, I have done

25
00:02:04,640 --> 00:02:13,100
with this task and starting a next one when we need to imagine this task of getting those files as well

26
00:02:13,100 --> 00:02:15,800
as the second one of setting up that file.

27
00:02:16,100 --> 00:02:18,200
Well, it might be very time consuming.

28
00:02:18,200 --> 00:02:18,460
Right.

29
00:02:18,980 --> 00:02:26,570
And imagine that if we have even ten users and there all the time using our application and if, for

30
00:02:26,570 --> 00:02:31,550
example, this one user does this one task or both of them.

31
00:02:32,530 --> 00:02:38,890
And they take really long time, what means that Noad is now going to be able to serve other users and

32
00:02:38,890 --> 00:02:42,220
trust me, it is at the end of the really, really big deal.

33
00:02:42,440 --> 00:02:46,360
And notice how JavaScript is reading this code synchronously.

34
00:02:46,990 --> 00:02:48,240
It just goes line by line.

35
00:02:48,490 --> 00:02:53,710
So we start with this task when we read these files and then we write the result.

36
00:02:53,740 --> 00:02:59,680
And again, if this is something that takes a really long time, then pretty much your application is

37
00:02:59,680 --> 00:03:00,050
down.

38
00:03:00,550 --> 00:03:06,160
So no other user can do anything with that application because one of the users is reading these two

39
00:03:06,160 --> 00:03:08,410
files and then writing one.

40
00:03:08,440 --> 00:03:13,960
Again, this is a very simple example, but you need to picture that, of course, there's going to

41
00:03:13,960 --> 00:03:16,840
be tasks that take longer time.

42
00:03:17,140 --> 00:03:21,110
And as an alternative, let's take a look at the async approach.

43
00:03:21,520 --> 00:03:24,580
So here again, we'll set up some simple console logs.

44
00:03:24,580 --> 00:03:27,670
And again, we'll cover this in a bigger detail a bit later.

45
00:03:28,060 --> 00:03:33,160
For the time being, let's just set up comes along and you'll see the biggest difference where I'm going

46
00:03:33,160 --> 00:03:34,330
to go with log.

47
00:03:35,170 --> 00:03:41,890
And let's say starts, I'm going to do that before I read this file, then I would want to go inside

48
00:03:41,890 --> 00:03:48,400
of the right file, the last one and sort of console login result, which, of course, is undefined,

49
00:03:48,400 --> 00:03:51,280
since in this case we are not getting anything back.

50
00:03:51,550 --> 00:03:56,710
Now, we're going to go with Don with this task.

51
00:03:57,040 --> 00:04:06,010
And then right after my first read file, I'm going to go with Log and starting next task.

52
00:04:06,490 --> 00:04:12,970
Hopefully you're able to file longer than before to read file inside of the right file and then right

53
00:04:12,970 --> 00:04:19,000
after the first read file, because, of course, the other ones are nested inside of this callback.

54
00:04:19,180 --> 00:04:28,660
If we go to node and if we just change it around to 11 and that FAA and I believe it was patient and

55
00:04:28,660 --> 00:04:33,640
if we run it noticed something cool where I have my start.

56
00:04:34,440 --> 00:04:41,570
And then right away, I have this starting next year and eventually I have this done with this task.

57
00:04:41,910 --> 00:04:49,380
So what this means is that since we have this asynchronous approach, the moment we start this task,

58
00:04:49,620 --> 00:04:50,700
we just offload.

59
00:04:51,270 --> 00:04:55,070
We just awful this task and we continue with our code.

60
00:04:55,380 --> 00:05:02,010
So that way when user number one, for example, comes here and wants to get this functionality.

61
00:05:02,850 --> 00:05:03,590
Yeah, sure.

62
00:05:03,900 --> 00:05:10,780
Here node just awful this task and then your application can keep on serving other users.

63
00:05:10,800 --> 00:05:15,510
Again, it seems a bit trivial at the moment because we have a basic approach.

64
00:05:15,810 --> 00:05:21,300
But hopefully you get an idea where in one case we have everything happening line by line.

65
00:05:21,520 --> 00:05:24,860
So only when we're done with the task, we're starting a next one.

66
00:05:25,380 --> 00:05:32,850
And with this approach, we start a task and we just offload the task and then we start a next task

67
00:05:32,850 --> 00:05:33,510
right away.

68
00:05:33,750 --> 00:05:38,760
And eventually, yes, we have done with this task and a console as well.

69
00:05:39,300 --> 00:05:42,660
Now, as far as the implementation, as far as the callbacks.

70
00:05:43,580 --> 00:05:45,320
Yes, this is a bit messy, right?

71
00:05:45,620 --> 00:05:51,640
Notice how we only want to accomplish a few tasks and it's already somewhat of a pain.

72
00:05:52,010 --> 00:05:57,770
Now alternatives are using promises as well as a sink away.

73
00:05:58,190 --> 00:06:04,550
Now, again, we understand that when we talk about reading the file as well as the writing on the file,

74
00:06:05,120 --> 00:06:12,500
I use this as an example of some kind of task that would take a long time now for the remainder of the

75
00:06:12,500 --> 00:06:12,800
course.

76
00:06:12,800 --> 00:06:18,910
Mostly it's going to be reading from the database, writing to the database and that sort of thing.

77
00:06:19,220 --> 00:06:24,860
But this is a very good illustration of how some task would take some time.

78
00:06:25,130 --> 00:06:27,440
So, of course, we would need to use a callback.

79
00:06:27,620 --> 00:06:33,320
And as far as the callbacks, yes, you can use them, but the code isn't going to get messy somewhat

80
00:06:33,320 --> 00:06:33,740
quickly.

81
00:06:34,150 --> 00:06:39,170
That's why a bit later in the course, we'll take a look at the alternatives, which are promises.

82
00:06:39,470 --> 00:06:45,710
And I think wait, I have to tell honestly, for the remainder of the course, we will use async await

83
00:06:45,710 --> 00:06:50,030
wait, because in my opinion, it is just easier to work with.

