1
00:00:06,520 --> 00:00:07,810
Welcome back.

2
00:00:08,170 --> 00:00:13,210
In this video, let's see how we can create our first promise.

3
00:00:13,210 --> 00:00:14,890
So let's get started.

4
00:00:17,710 --> 00:00:22,870
Well, let's have a look at the syntax of creating a promise.

5
00:00:23,110 --> 00:00:27,790
Well, let's go through this syntax line by line.

6
00:00:27,910 --> 00:00:32,180
We have a keyword called concept and the variable called promise.

7
00:00:32,200 --> 00:00:34,130
This variable can be anything.

8
00:00:34,150 --> 00:00:34,840
Right.

9
00:00:34,840 --> 00:00:38,730
And the clue part is what comes after the e cosine.

10
00:00:38,740 --> 00:00:47,590
We see we have a new keyword, which is a special keyword in JavaScript and we have promise for this

11
00:00:47,590 --> 00:00:48,240
promise.

12
00:00:48,250 --> 00:00:49,210
It's a function.

13
00:00:49,210 --> 00:00:53,440
Then we pass in only one element to this function.

14
00:00:53,890 --> 00:00:55,210
So this is my agreement.

15
00:00:55,210 --> 00:01:01,960
I pass in to the promise function and this one is what we call callback.

16
00:01:01,960 --> 00:01:04,540
For this one, I use every function.

17
00:01:04,900 --> 00:01:11,380
So inside a callback you see that we pass in to agreement, resolve and reject.

18
00:01:11,620 --> 00:01:15,880
So resolve and reject the ah functions well.

19
00:01:15,880 --> 00:01:19,930
So inside our promise you see that your code goes here.

20
00:01:19,930 --> 00:01:22,660
And then we call this resolve.

21
00:01:22,660 --> 00:01:28,720
If the promise is resolved and we call reject when the promise is rejected, great.

22
00:01:28,720 --> 00:01:35,960
So here it means that we are waiting to implement a code that takes some time to execute, like making

23
00:01:36,290 --> 00:01:37,600
HTTP calls.

24
00:01:37,600 --> 00:01:43,330
So in this context, we are going to mimic assuming that we are making HTTP requests.

25
00:01:44,110 --> 00:01:49,330
So let's see how we can create our first promise using code.

26
00:01:49,360 --> 00:01:51,150
So back to Visual City code.

27
00:01:51,160 --> 00:01:52,360
So let me comment.

28
00:01:52,360 --> 00:01:59,140
This one's that is a bad callback and now let me copy this one and come down here.

29
00:01:59,170 --> 00:02:02,560
This one going to be a promise.

30
00:02:02,800 --> 00:02:04,690
And here we go.

31
00:02:04,690 --> 00:02:06,820
How can we create a promise?

32
00:02:07,150 --> 00:02:16,750
So first of all, we bring new key word like that and then promise, which is a special keyword in JavaScript.

33
00:02:16,750 --> 00:02:21,340
And then inside that we pass in a callback function.

34
00:02:21,340 --> 00:02:29,980
I can pass in normal function as this or I can make use of arrow function so the choice is yours.

35
00:02:29,980 --> 00:02:32,290
So I'm going to use arrow function.

36
00:02:32,290 --> 00:02:35,830
So this function takes in only one argument.

37
00:02:35,830 --> 00:02:40,870
That's a callback function, that is an anonymous function, a function without a name.

38
00:02:41,230 --> 00:02:51,130
So inside our callback function here, we need to have access these two again main core result and then

39
00:02:51,130 --> 00:02:54,820
reject as we discussed in the previous video.

40
00:02:55,090 --> 00:03:05,710
So inside of function body now our code goes here well so remember that the resolve and then reject

41
00:03:05,710 --> 00:03:06,850
the function.

42
00:03:06,850 --> 00:03:14,170
That's why we are callback remember how we able to identify that is a callback so I can name this one

43
00:03:14,170 --> 00:03:21,910
whatever I want I can see that JavaScript or CSS or my my resolve or rejected or error or okay it can

44
00:03:21,910 --> 00:03:23,050
be anything.

45
00:03:23,620 --> 00:03:28,690
So here we have two functions and remember promise.

46
00:03:28,690 --> 00:03:32,410
Always retain either resolve or reject.

47
00:03:32,440 --> 00:03:38,200
You cannot retain two of them, let's say resolve or vector, because if you make a request, there

48
00:03:38,200 --> 00:03:41,800
are chances that we can have success or error.

49
00:03:41,800 --> 00:03:43,510
We cannot have error in success.

50
00:03:43,510 --> 00:03:48,580
So remember that we always retain one function from a promise.

51
00:03:48,580 --> 00:03:51,700
So how can we do it based on a condition?

52
00:03:51,910 --> 00:03:53,620
So what comes back?

53
00:03:53,620 --> 00:03:56,110
We need to assign to a variable.

54
00:03:56,140 --> 00:03:58,600
It can be anything by convention.

55
00:03:58,600 --> 00:04:02,170
We make this of promise equal to that.

56
00:04:02,170 --> 00:04:07,060
So with this one I have created my first promise for this one.

57
00:04:07,060 --> 00:04:10,420
There is no syntax or there's no logic going on.

58
00:04:10,900 --> 00:04:13,450
So let's say that we are making HTTP requests.

59
00:04:13,450 --> 00:04:17,230
It simply means that a request that takes some time before we can get it.

60
00:04:17,230 --> 00:04:23,560
So let's say that you want to fetch all users, let's say a user from database, so we can say that

61
00:04:23,560 --> 00:04:26,560
user is equal to simple object.

62
00:04:26,770 --> 00:04:28,480
And here you go.

63
00:04:29,020 --> 00:04:34,000
So let's say the name of this user name is going to be John.

64
00:04:34,810 --> 00:04:41,230
And then let's say the city of this person is commands can be anything of your choice.

65
00:04:41,350 --> 00:04:44,170
So this is our logic of fetching that.

66
00:04:44,170 --> 00:04:46,750
We are fetching this one from database.

67
00:04:47,230 --> 00:04:56,830
Okay, so let's have a variable called let's say it's fetched, so let's is fetched, meaning that we

68
00:04:56,830 --> 00:04:59,740
have first the data from database.

69
00:04:59,740 --> 00:05:05,110
Based on this condition, we can render any of these ones good.

70
00:05:05,110 --> 00:05:07,750
So here we are going to retain one of these.

71
00:05:07,750 --> 00:05:12,790
It means that if it's fetched, meaning we have back, we have the user.

72
00:05:12,790 --> 00:05:27,280
So here we will say that if is first is first, then we can console.log the resolve otherwise or else

73
00:05:28,270 --> 00:05:33,340
reject and then I'll bring reject here as that.

74
00:05:33,340 --> 00:05:38,980
So we see that we are turning one of these based on these conditions here.

75
00:05:39,220 --> 00:05:43,300
So now we have created our perfect promise.

76
00:05:43,300 --> 00:05:45,880
So how can we use it?

77
00:05:45,880 --> 00:05:54,640
The next video we will talk more about the resolve and then reject in the next video as well.

