1
00:00:06,500 --> 00:00:07,890
Welcome back.

2
00:00:07,910 --> 00:00:13,340
And this video is all about async await.

3
00:00:13,550 --> 00:00:20,390
Well, async await is also another way of writing async code.

4
00:00:20,780 --> 00:00:27,320
Now, you talk about callbacks, promises, and now we are on async await.

5
00:00:27,710 --> 00:00:33,890
Async await is most commonly used among promises and callbacks.

6
00:00:33,890 --> 00:00:40,670
So in this video, let's see how we are going to make use of async await, which is a new feature that

7
00:00:40,670 --> 00:00:43,550
has been shaped into the JavaScript language.

8
00:00:43,700 --> 00:00:48,400
So how can we use async await and when do we need to use it?

9
00:00:48,410 --> 00:00:54,950
So in this particular video, let's see in details what is saying await is or about.

10
00:00:55,370 --> 00:01:03,020
Before we get into it, let's look at the syntax of writing is saying I wait for this particular function,

11
00:01:03,020 --> 00:01:05,390
let's see what is going on here.

12
00:01:05,690 --> 00:01:14,570
You could see that we have a special keyword called async and then after that we bring our function

13
00:01:14,570 --> 00:01:19,000
keyword and the function name where and inside the function.

14
00:01:19,010 --> 00:01:26,900
The difference here is we have a new keyword called await and then we have a function call, for example,

15
00:01:26,900 --> 00:01:28,730
make API requests.

16
00:01:28,970 --> 00:01:35,840
So before we get into this, how we are going to code from scratch using async await, let's have a

17
00:01:35,840 --> 00:01:39,530
look at the rules governing async await.

18
00:01:39,680 --> 00:01:47,480
The first point says that it adjusts like a syntactic sugar for promises, meaning that whether it's

19
00:01:47,480 --> 00:01:51,410
a promise or async await, they have the same logic.

20
00:01:51,410 --> 00:01:53,450
But the syntax that differs.

21
00:01:53,480 --> 00:01:56,000
Nothing has been changed into the logic.

22
00:01:56,030 --> 00:01:59,360
Everything is the same, but the syntax that differ.

23
00:01:59,360 --> 00:02:04,040
That is why we say is that just like a syntactic sugar for promises.

24
00:02:04,040 --> 00:02:12,020
And apart from that you also have the weight keyword and it's only valid inside async functions.

25
00:02:12,050 --> 00:02:18,710
It simply means that we only use a weight when we mark our function as an async.

26
00:02:18,710 --> 00:02:24,710
So coming back to this, you could say that for this particular function we have async.

27
00:02:24,710 --> 00:02:28,580
That's why we are able to use a weight inside this function.

28
00:02:28,580 --> 00:02:36,350
So again, a weight is valid inside async function and its function is a function that has a special

29
00:02:36,350 --> 00:02:39,080
keyword called async before the function name.

30
00:02:39,080 --> 00:02:40,670
So let's continue.

31
00:02:40,670 --> 00:02:47,090
Well, the next point is that async function must start with async keyword, and that is what we have

32
00:02:47,090 --> 00:02:47,540
done.

33
00:02:47,540 --> 00:02:49,820
We have what, async here.

34
00:02:49,910 --> 00:02:51,550
So let's continue on.

35
00:02:51,680 --> 00:02:59,720
The next point is that a weight is put in front of any promise based API call or function that will

36
00:02:59,720 --> 00:03:01,700
take some time to complete.

37
00:03:01,700 --> 00:03:08,450
So the trick part here is that sometimes we get confused when to use a weight.

38
00:03:08,600 --> 00:03:17,240
Well, the key note here is that we use a weight in front of any function or any code that needs some

39
00:03:17,240 --> 00:03:21,440
time to complete, for example, making it HTTP calls.

40
00:03:21,530 --> 00:03:27,770
So if we say that we have a function called make API call request, and for this one we're going to

41
00:03:27,770 --> 00:03:28,850
take some time.

42
00:03:28,880 --> 00:03:32,300
That's why we added a weight in front of it.

43
00:03:32,300 --> 00:03:40,220
So any code in your code base that needs some time to execute, you need to mark or put a weight in

44
00:03:40,220 --> 00:03:41,180
front of it.

45
00:03:41,180 --> 00:03:44,030
So our simply means that I'm waiting for you.

46
00:03:44,060 --> 00:03:49,580
Just go and make the request when you finish, then I'll give back the value and assigned a variable

47
00:03:49,580 --> 00:03:50,720
called response.

48
00:03:50,750 --> 00:03:54,680
That is it about the weight keyword what it does.

49
00:03:54,860 --> 00:04:03,260
The next point is that async function returns a promise that one we've talked about it before and promises

50
00:04:03,350 --> 00:04:06,320
always async function returns a promise.

51
00:04:06,320 --> 00:04:11,810
So in that case we need to watch, consume or resolve it, use it to then when it goes to, let's say

52
00:04:11,810 --> 00:04:19,550
the promises you use then and cache for that but for async await, let's see what we need.

53
00:04:19,550 --> 00:04:24,260
Great for async await we use try and catch again.

54
00:04:24,260 --> 00:04:30,260
Try and catch to handle success and error, remember.

55
00:04:30,260 --> 00:04:38,660
So in case of promise we make keys of resolve when a promise is success, but for async await instead

56
00:04:38,660 --> 00:04:44,900
of resolve, we're going to use try and instead of reject in promise, we are going to use catch to

57
00:04:44,900 --> 00:04:47,000
handle those responses.

58
00:04:47,000 --> 00:04:56,840
That comes back when you make an API requests and lastly use async, await to wait for the promise to

59
00:04:56,840 --> 00:04:58,850
resolve like I've said.

60
00:04:58,850 --> 00:05:06,020
All right, so with this one in mind in this video, let's see how we are going to write our first async.

61
00:05:06,180 --> 00:05:07,320
With passion.

62
00:05:07,320 --> 00:05:09,210
Let's get rocking the next video.

