1 00:00:00,500 --> 00:00:07,100 So finally, I'm back to my Visual Studio Code editor, and you can see I create a HTML file name index 2 00:00:07,100 --> 00:00:08,270 dot HTML. 3 00:00:08,930 --> 00:00:14,320 And I also open this file in my browser using live server extension inside the head tag. 4 00:00:14,330 --> 00:00:18,770 First I'm going to type script tag and inside the script tag. 5 00:00:18,770 --> 00:00:25,970 First I'm going to take a variable late complete and its value is true. 6 00:00:26,510 --> 00:00:31,190 It's a boolean value and I'm going to check this value in our promise method. 7 00:00:31,670 --> 00:00:35,360 We will do our next code depending on this result. 8 00:00:35,450 --> 00:00:37,790 Then I'm going to create a promise method. 9 00:00:37,820 --> 00:00:40,310 So I'm going to take a variable named Pro. 10 00:00:40,730 --> 00:00:45,650 And then I'm going to use new keyword new promise. 11 00:00:46,160 --> 00:00:52,220 And inside this promise method we have to create a function and I type function keyword. 12 00:00:52,610 --> 00:00:57,680 Then inside the curly braces we need to create conditions. 13 00:00:57,860 --> 00:01:00,710 So I'm going to create if condition. 14 00:01:00,710 --> 00:01:06,230 If inside the parentheses I am going to check our variable value. 15 00:01:06,710 --> 00:01:09,530 So I type my variable name complete. 16 00:01:09,920 --> 00:01:17,210 If true it redirect to if condition and if false then it redirect to else condition. 17 00:01:17,690 --> 00:01:24,260 As I told you before, promise return only to result it's return reject or fulfill. 18 00:01:24,860 --> 00:01:29,090 For this we need to take two parameter in our function. 19 00:01:29,300 --> 00:01:30,740 For successful. 20 00:01:31,160 --> 00:01:37,730 I am going to take result variable and for reject I'm going to take reject variable. 21 00:01:37,730 --> 00:01:46,340 If our function is satisfied then inside the if condition I'm going to use resolve function and if reject 22 00:01:46,820 --> 00:01:50,720 insert the else condition I'm going to call reject function. 23 00:01:51,200 --> 00:01:54,950 We don't need to create this function, we just need to call this function. 24 00:01:54,950 --> 00:02:02,570 If we call this function then we can use our inbuilt functions which is then and catch. 25 00:02:02,720 --> 00:02:06,140 And now I'm going to send message for resolve condition. 26 00:02:06,140 --> 00:02:09,050 I am successful for reject. 27 00:02:09,050 --> 00:02:11,270 I am going to send another message. 28 00:02:12,050 --> 00:02:17,480 I am failed and now I am going to call this Pro object in our console. 29 00:02:18,200 --> 00:02:22,280 Console dot log inside the round Braces Pro. 30 00:02:22,730 --> 00:02:25,610 It's not a normal function, it's an object. 31 00:02:25,700 --> 00:02:29,780 So we don't need to use round braces after object name. 32 00:02:30,020 --> 00:02:35,630 And if I save this file you can see in my console section you can see in my console it print. 33 00:02:35,660 --> 00:02:41,720 I am successful because we sent true value in our complete variable. 34 00:02:41,720 --> 00:02:44,570 And if I open it you can see promise result. 35 00:02:44,570 --> 00:02:47,780 I am successful and promise state fulfilled. 36 00:02:48,200 --> 00:02:56,900 But if I set false value to our variable and save this file, you can see a message I am failed and 37 00:02:56,900 --> 00:02:58,340 also it's showing an error. 38 00:02:58,760 --> 00:03:02,900 And if I open this promise, you can see promise state rejected. 39 00:03:03,500 --> 00:03:07,010 Also we can send parameter like normal function. 40 00:03:07,640 --> 00:03:10,340 So I'm going to remove this complete variable value. 41 00:03:10,730 --> 00:03:17,900 And I'm going to create a function function pro inside the round braces complete. 42 00:03:17,990 --> 00:03:19,940 Then inside the curly braces. 43 00:03:19,940 --> 00:03:23,300 And I'm going to move this code inside the curly braces. 44 00:03:23,720 --> 00:03:26,570 So our function going to return promise. 45 00:03:26,570 --> 00:03:29,510 So for now we don't need to take variables. 46 00:03:29,510 --> 00:03:34,250 So I'm going to remove this variable name and return new promise. 47 00:03:34,790 --> 00:03:37,760 And we need to pass value when we call this function. 48 00:03:38,270 --> 00:03:46,400 If I send true as an argument if I save this file you can see in my console it print I am successful. 49 00:03:46,670 --> 00:03:52,130 And if I send false as an argument now it's print I am failed. 50 00:03:52,460 --> 00:03:54,890 So our promise function worked properly. 51 00:03:55,130 --> 00:04:00,080 And now I'm going to create two different functions for resolve and for reject. 52 00:04:00,110 --> 00:04:05,360 For resolve I'm going to create a variable for late on fulfillment. 53 00:04:05,360 --> 00:04:08,060 And I'm going to use arrow function. 54 00:04:08,240 --> 00:04:13,400 And when we call this function I'm going to send a value which is result. 55 00:04:13,400 --> 00:04:21,020 And this value came from resolve function inside the curly braces console dot log result. 56 00:04:21,020 --> 00:04:25,010 Similarly we need to create another function for reject section. 57 00:04:25,400 --> 00:04:35,090 So I duplicate this lines and replace on fulfillment with on rejection and send error as in parameter 58 00:04:36,080 --> 00:04:38,720 console dot log print error. 59 00:04:38,990 --> 00:04:46,310 And as I told you before, we need to use callback function to call this functions which are then end 60 00:04:46,310 --> 00:04:47,060 catch. 61 00:04:47,840 --> 00:04:50,360 But first we need to call pro function. 62 00:04:51,080 --> 00:04:57,440 And inside the parentheses we need to send a value and our value is true dot. 63 00:04:57,920 --> 00:04:59,630 Then if our. 64 00:04:59,760 --> 00:05:02,910 Problem is resolved inside the parentheses. 65 00:05:03,360 --> 00:05:06,060 We need to call on fulfillment function. 66 00:05:06,720 --> 00:05:10,200 And I'm going to duplicate this line for rejection purpose. 67 00:05:10,740 --> 00:05:13,770 And this time we need to use catch function. 68 00:05:14,860 --> 00:05:19,240 And inside the parentheses we need to call on rejection function. 69 00:05:19,750 --> 00:05:23,380 And for now I don't need this console.log section. 70 00:05:23,380 --> 00:05:24,790 So I'm going to remove it. 71 00:05:24,820 --> 00:05:29,170 If I save this file you can see in my console it print I am successful. 72 00:05:29,470 --> 00:05:32,750 As I told you, promise has three stages. 73 00:05:32,770 --> 00:05:36,550 First one is pending, then resolve and reject. 74 00:05:36,760 --> 00:05:38,560 Let's talk about pending. 75 00:05:39,490 --> 00:05:41,470 We use it before promise start. 76 00:05:42,430 --> 00:05:48,670 So when I return the function inside the function first I'm going to type console dot log. 77 00:05:49,420 --> 00:05:50,500 Please wait. 78 00:05:51,100 --> 00:05:53,410 Now it is on pending stage. 79 00:05:54,400 --> 00:05:57,400 And then I'm going to use set timeout function. 80 00:05:58,180 --> 00:05:59,620 Set timeout. 81 00:05:59,920 --> 00:06:02,110 And then I'm going to create arrow function. 82 00:06:02,650 --> 00:06:04,480 I made a little mistake. 83 00:06:04,810 --> 00:06:08,410 We have to write the pending stage inside the promise function. 84 00:06:08,920 --> 00:06:17,170 So I am going to select this section and move it inside the promise method inside the set timeout section. 85 00:06:17,650 --> 00:06:19,780 I'm going to pass this if condition. 86 00:06:20,470 --> 00:06:24,160 And I want to pass this condition for 4000 milliseconds. 87 00:06:24,760 --> 00:06:28,630 And if I save this file you can see in my console first it print. 88 00:06:28,660 --> 00:06:29,590 Please wait. 89 00:06:30,100 --> 00:06:35,530 And we need to wait for four second but then it print I am successful. 90 00:06:35,860 --> 00:06:36,580 It print. 91 00:06:36,580 --> 00:06:40,040 Please wait twice to resolve this problem. 92 00:06:40,060 --> 00:06:43,780 I am going to use method chaining and make it. 93 00:06:43,780 --> 00:06:48,790 Then in catch function one liner I am going to remove this catch function. 94 00:06:49,300 --> 00:06:54,680 And after unfulfilment dot catch on rejection. 95 00:06:54,700 --> 00:07:03,790 Now if result is true, it is called all fulfillment function and if false it call on rejection function. 96 00:07:03,790 --> 00:07:06,790 And if I save this file you can see in my console section. 97 00:07:06,940 --> 00:07:07,960 First it print. 98 00:07:07,960 --> 00:07:08,800 Please wait. 99 00:07:09,190 --> 00:07:12,760 So we need to wait for four second, then it print. 100 00:07:12,760 --> 00:07:20,650 I am successful and similarly if I pass false parameter and save this file, we need to wait for four 101 00:07:20,650 --> 00:07:21,250 second. 102 00:07:22,400 --> 00:07:24,440 And then it print I am failed. 103 00:07:25,070 --> 00:07:25,520 Please. 104 00:07:25,520 --> 00:07:27,500 What is our pending message? 105 00:07:27,950 --> 00:07:34,010 We use a timeout function because it is take a long time to came data from the server. 106 00:07:34,160 --> 00:07:35,870 Let me show you one thing. 107 00:07:35,870 --> 00:07:39,590 If you do not want to create this function, you can. 108 00:07:39,590 --> 00:07:44,630 In that case, you directly call this function in then or catch section. 109 00:07:45,440 --> 00:07:50,810 So I'm going to copy this result function and call it inside the then function. 110 00:07:51,350 --> 00:07:55,070 And similarly I call error function in our catch function. 111 00:07:55,550 --> 00:07:57,950 And I'm going to comment out this previous line. 112 00:07:58,490 --> 00:08:03,320 And if I save this file first it run our pending stage. 113 00:08:03,440 --> 00:08:04,550 Then it print. 114 00:08:04,550 --> 00:08:09,710 I am failed because we send false value to our Pro function. 115 00:08:10,460 --> 00:08:15,110 Not only that we can pass multiple argument to our Pro function. 116 00:08:15,410 --> 00:08:18,500 For now, I don't need this line, so I'm going to remove it. 117 00:08:18,500 --> 00:08:23,150 And inside my pro function I am going to pass two parameter. 118 00:08:23,780 --> 00:08:25,070 Two and seven. 119 00:08:25,790 --> 00:08:30,890 And inside my function I'm going to take two parameters x and y. 120 00:08:31,700 --> 00:08:36,080 Then inside the promise I'm going to take another variable z. 121 00:08:36,260 --> 00:08:38,420 Z equal to x. 122 00:08:38,420 --> 00:08:39,650 Multiply by y. 123 00:08:40,070 --> 00:08:42,410 Then insert the set timeout function. 124 00:08:42,960 --> 00:08:46,850 If complete variable, replace with z. 125 00:08:47,090 --> 00:08:51,600 If multiplication is possible, then insert the result function. 126 00:08:51,620 --> 00:09:00,800 I'm going to use template string method inside the Backticks your result colon dollar sign inside the 127 00:09:00,800 --> 00:09:01,760 curly braces. 128 00:09:02,030 --> 00:09:04,910 Z means our result. 129 00:09:05,150 --> 00:09:11,210 Similarly, inside the reject function I'm going to send wrong input. 130 00:09:11,600 --> 00:09:15,980 If I save this file first we need to wait for four second. 131 00:09:16,280 --> 00:09:21,680 Then it print our result 14 because we pass two numeric value. 132 00:09:21,680 --> 00:09:23,290 So our condition is true. 133 00:09:23,300 --> 00:09:27,290 But if we pass a string value then what happened. 134 00:09:27,590 --> 00:09:33,620 So I'm going to pass a string value as an argument seven replace with small t. 135 00:09:34,320 --> 00:09:40,500 And if I save this file now you can see in my console it's print wrong input. 136 00:09:41,040 --> 00:09:44,250 Basically we handle errors using promise method. 137 00:09:44,580 --> 00:09:51,960 I hope now you understand what is promise in advanced JavaScript and why and how we use it. 138 00:09:52,350 --> 00:09:54,060 Thanks for watching this video. 139 00:09:54,150 --> 00:09:56,040 See you on the next tutorial.