1
00:00:00,870 --> 00:00:02,400
Instructor: The first technique we'll discuss

2
00:00:02,400 --> 00:00:05,370
is the Async Operations.

3
00:00:05,370 --> 00:00:07,680
The basic premise of Async Operations

4
00:00:07,680 --> 00:00:10,290
is that the code should not wait

5
00:00:10,290 --> 00:00:13,980
for long operations to complete, but instead

6
00:00:13,980 --> 00:00:18,210
the code begins a long operation and continues on

7
00:00:18,210 --> 00:00:22,110
and when the operation is done, then the code can continue

8
00:00:22,110 --> 00:00:25,740
from the point where the operation completed.

9
00:00:25,740 --> 00:00:29,280
Now, this is relevant mainly for IO operations

10
00:00:29,280 --> 00:00:31,140
such as operations related

11
00:00:31,140 --> 00:00:35,430
to files, to database access, to networking, et cetera.

12
00:00:35,430 --> 00:00:39,000
So if you have an operation that requires reading

13
00:00:39,000 --> 00:00:42,240
dozens of files, then using the Async Operation

14
00:00:42,240 --> 00:00:46,260
you can instruct the code to begin the reading operation.

15
00:00:46,260 --> 00:00:50,250
Meanwhile, continuing performing other operations

16
00:00:50,250 --> 00:00:52,500
and when the file reading is complete

17
00:00:52,500 --> 00:00:54,930
you can continue straight from the point

18
00:00:54,930 --> 00:00:57,750
in the code where the reading is done.

19
00:00:57,750 --> 00:01:00,480
Now, almost every development platforms

20
00:01:00,480 --> 00:01:05,480
supports this concept nowadays, and let's see an example.

21
00:01:05,489 --> 00:01:07,740
And what you see here is an implementation

22
00:01:07,740 --> 00:01:11,010
of the Async await pattern in node.js.

23
00:01:11,010 --> 00:01:13,110
There are two things to know in this code.

24
00:01:13,110 --> 00:01:16,860
First is the a wait keyword

25
00:01:16,860 --> 00:01:20,190
which signals the code to begin this operations

26
00:01:20,190 --> 00:01:23,940
the fetch operation, which is a long operation.

27
00:01:23,940 --> 00:01:26,850
And while the code is fetching the data

28
00:01:26,850 --> 00:01:29,010
from this address, then the calling method

29
00:01:29,010 --> 00:01:32,045
can continue doing whatever they did before.

30
00:01:32,045 --> 00:01:36,540
Now for this to happen, there is also the Async keyword.

31
00:01:36,540 --> 00:01:40,680
So the Async keyword, signals the calling method

32
00:01:40,680 --> 00:01:44,970
that this function, the Async Ajax await function

33
00:01:44,970 --> 00:01:46,620
is an Async function,

34
00:01:46,620 --> 00:01:49,830
which means it might have an await Q ordinate

35
00:01:49,830 --> 00:01:54,090
so the calling function might continue do something

36
00:01:54,090 --> 00:01:57,090
after calling the Async Ajax await.

37
00:01:57,090 --> 00:01:59,790
And again, the I Async await pattern exists

38
00:01:59,790 --> 00:02:02,730
in almost every development platform.

39
00:02:02,730 --> 00:02:05,310
This pattern is a very powerful one

40
00:02:05,310 --> 00:02:08,009
and you should always aspire to use it.

41
00:02:08,009 --> 00:02:10,470
It frees the threads on the web server

42
00:02:10,470 --> 00:02:14,013
and it dramatically improves the performance of the API.

