1 00:00:01,820 --> 00:00:02,570 Welcome back. 2 00:00:03,110 --> 00:00:11,910 Today, we're going to learn about E.S. 11 or is 20, 20, so this ECMAScript was released in 2020. 3 00:00:12,110 --> 00:00:17,850 So congratulations, ladies and gentlemen, you are very, very successful. 4 00:00:18,350 --> 00:00:21,560 So we started from the scratch and now we are here. 5 00:00:22,610 --> 00:00:26,180 But today we're not going to jump straight to iOS 11 features. 6 00:00:26,480 --> 00:00:32,270 So I'm going to go back to Essex and I'm going to mention four methods for promises that we haven't 7 00:00:32,270 --> 00:00:32,630 covered. 8 00:00:32,630 --> 00:00:39,680 And we haven't covered this for a reason, because in the year 2020, there is a new method released 9 00:00:39,680 --> 00:00:40,730 for these promises. 10 00:00:40,910 --> 00:00:49,430 And I knew then and I knew it now that if I just include it in one place instead of including 50 lecture's 11 00:00:49,430 --> 00:00:52,840 before, then you will pretty much forget about it. 12 00:00:53,210 --> 00:00:56,600 And we don't we didn't use them until now, maybe. 13 00:00:56,600 --> 00:01:02,550 Actually, I've used it only once in, but that was not something very important. 14 00:01:02,570 --> 00:01:07,600 So I haven't used these six features, but I'm going to use it now. 15 00:01:07,610 --> 00:01:08,840 I'm going to use them now. 16 00:01:09,170 --> 00:01:16,030 And I need to show you all of this because I will not be able to explain all of the year 2020 features. 17 00:01:16,370 --> 00:01:19,730 So you haven't missed out anything before. 18 00:01:19,940 --> 00:01:23,440 Right now it's the time to start working on them. 19 00:01:23,450 --> 00:01:24,090 So let's start. 20 00:01:24,680 --> 00:01:25,760 So is six. 21 00:01:27,800 --> 00:01:29,700 So these are your six promises. 22 00:01:30,140 --> 00:01:37,790 So before you six implementation, we have a promise is implemented in a libraries like Bluebird or 23 00:01:37,790 --> 00:01:38,750 Q Right. 24 00:01:38,800 --> 00:01:47,480 Thank now now S6 or is 2015 promises are implemented as a standard standard part of JavaScript core 25 00:01:47,480 --> 00:01:48,010 language. 26 00:01:48,320 --> 00:01:51,890 So we have old method race method to reject and result in. 27 00:01:51,890 --> 00:01:54,800 These are implemented in promises class. 28 00:01:54,830 --> 00:02:00,380 So this is promise class nice so we can make more instances of now promise. 29 00:02:00,380 --> 00:02:05,090 The old method takes in the iterable all the promises as an input. 30 00:02:05,120 --> 00:02:06,980 So that can be a real overpromises. 31 00:02:06,980 --> 00:02:07,250 Right. 32 00:02:07,520 --> 00:02:12,890 And returns back a single promise that resolves to an array of results. 33 00:02:13,910 --> 00:02:14,330 What. 34 00:02:14,660 --> 00:02:16,810 OK, so let's start coding. 35 00:02:17,330 --> 00:02:19,130 So first things first. 36 00:02:19,130 --> 00:02:26,720 I'm going to create const promise one equal new promise here so we make sense of this class here. 37 00:02:26,720 --> 00:02:32,300 Are we going to see whether it will be resolved or rejected? 38 00:02:33,680 --> 00:02:40,190 So I'm going to say resolve or reject here and I'm going to pass the error function in inside here. 39 00:02:40,190 --> 00:02:45,530 We're going to mention the set timeout function right now. 40 00:02:45,530 --> 00:02:50,060 This set time, our function is super important for us. 41 00:02:50,390 --> 00:02:50,710 Why? 42 00:02:50,720 --> 00:02:58,940 Because we haven't used it until now cuz now the set out function we haven't used actually in years 43 00:02:58,940 --> 00:03:04,870 since before we used it, when I was explaining the promise in this set time, I will never feel right. 44 00:03:05,090 --> 00:03:06,400 It will always execute. 45 00:03:06,410 --> 00:03:08,530 It's not like an API that can go wrong. 46 00:03:09,110 --> 00:03:16,550 That's why here we are going to pass the resolve in the milliseconds when they should kick in. 47 00:03:16,700 --> 00:03:19,340 And this is the value that I want to restore. 48 00:03:19,820 --> 00:03:20,630 Promise one. 49 00:03:28,750 --> 00:03:37,000 Let's compare this promise based at two times and change this to two as well here, too, 50 00:03:40,720 --> 00:03:45,520 and the last one to three and change it back to three. 51 00:03:46,210 --> 00:03:47,650 Now, what's the Syntex? 52 00:03:47,650 --> 00:03:49,980 Let's be sure that you put a semicolon there. 53 00:03:49,990 --> 00:03:50,690 You don't have to. 54 00:03:51,040 --> 00:03:52,270 That what's the syntax. 55 00:03:52,450 --> 00:03:56,110 So promise that all in here is. 56 00:03:56,110 --> 00:03:57,910 I told you it takes an iterable. 57 00:03:57,910 --> 00:04:01,300 Overpromises or an array is an input in here. 58 00:04:01,300 --> 00:04:02,680 I'm going to jam all of this. 59 00:04:02,680 --> 00:04:04,840 Promise one. 60 00:04:05,410 --> 00:04:10,390 Promise to and promise three. 61 00:04:11,050 --> 00:04:11,440 Right. 62 00:04:12,070 --> 00:04:12,370 Good. 63 00:04:13,210 --> 00:04:16,500 And what I can do here, I'm going to attach the then method. 64 00:04:19,290 --> 00:04:27,990 And I'm going to simply data and control this data so we can move this one a little bit down here and 65 00:04:27,990 --> 00:04:33,270 I'm going to console lock the data, that is pretty much what I can do here. 66 00:04:34,560 --> 00:04:36,260 Actually, let's not finish it here. 67 00:04:36,270 --> 00:04:38,190 Attach that catch. 68 00:04:38,640 --> 00:04:43,980 And in this catch here, I'm going to parse the error like that. 69 00:04:46,420 --> 00:04:59,110 And simply, I'm going to counsel and I'm going to see error occurred in one of the promises above. 70 00:05:04,230 --> 00:05:10,880 Space here, exclamation mark, and as well, I'm going to just print out the air nice. 71 00:05:11,790 --> 00:05:15,430 So these all three promises are resolved. 72 00:05:15,450 --> 00:05:16,770 So here is the syntax. 73 00:05:16,770 --> 00:05:21,950 Is this Brahmas that all Jammal, the promise is here as an input or as an array? 74 00:05:22,230 --> 00:05:27,030 And here I'm not doing anything special on this, bringing out the data or if it's any error I'm going 75 00:05:27,030 --> 00:05:27,450 to catch. 76 00:05:27,450 --> 00:05:31,530 But I know it will not be error because this is a satima function. 77 00:05:31,740 --> 00:05:34,050 It will always resolve the truth. 78 00:05:34,830 --> 00:05:35,220 Right. 79 00:05:35,530 --> 00:05:38,990 If we don't specify reject here, it will always be true. 80 00:05:39,000 --> 00:05:40,620 But here we need to change one thing. 81 00:05:41,130 --> 00:05:43,250 It's a five hundred milliseconds for the first one. 82 00:05:43,530 --> 00:05:45,060 Make thousand loops. 83 00:05:46,350 --> 00:05:47,970 Make thousand for and a second. 84 00:05:48,360 --> 00:05:51,210 Second one and let's make two thousand for the third one. 85 00:05:51,330 --> 00:05:53,730 These are all in milliseconds. 86 00:05:54,150 --> 00:05:55,620 OK, copied interring. 87 00:05:56,520 --> 00:05:57,990 Go to Google Chrome. 88 00:05:57,990 --> 00:06:05,430 I suggest you to use the Google Chrome based in here and enter now the promise will be pending and now 89 00:06:05,850 --> 00:06:14,880 all of the promises are resolved in the output, as I told you, is an array of resolved promises. 90 00:06:15,240 --> 00:06:17,880 So we have promised one promise to promise three. 91 00:06:18,360 --> 00:06:18,710 Cool. 92 00:06:19,530 --> 00:06:23,760 But what what will happen if I reject one of them here? 93 00:06:25,900 --> 00:06:39,720 You know, OK, save it, refresh F5 or hit here, spending Arakwal in one of the promise and I'm catching 94 00:06:39,720 --> 00:06:50,760 that everything I promise to where we have that promise rejected, now it reject immediately upon any 95 00:06:50,760 --> 00:06:57,240 of the input promises we rejected and where or where there is a throne in there and it will reject with 96 00:06:57,240 --> 00:07:00,780 the first rejection message or the error that we just got. 97 00:07:01,620 --> 00:07:05,850 So the problem is that this is something that you shouldn't need. 98 00:07:06,120 --> 00:07:13,320 You should know if we have any rejection here, it will stop of executing and it will throw it immediately. 99 00:07:14,310 --> 00:07:17,670 So that is all that you need to know about promise. 100 00:07:17,820 --> 00:07:21,570 Oh, now I have another one interesting promise race. 101 00:07:22,440 --> 00:07:28,070 And actually here I'm going to keep it interesting, same as this one. 102 00:07:28,470 --> 00:07:38,120 We're going to make a couple of changes here, one, two and three right now. 103 00:07:38,640 --> 00:07:41,190 We also need to change this promise, that race. 104 00:07:41,410 --> 00:07:44,740 So first, let me write it down and I'm going to explain what will happen here. 105 00:07:45,310 --> 00:07:50,160 Now, let's go back here and see now. 106 00:07:50,160 --> 00:07:51,030 Promise one. 107 00:07:51,450 --> 00:07:52,980 It's equal to the new promise. 108 00:07:53,310 --> 00:07:54,390 We will resolve them. 109 00:07:54,660 --> 00:07:58,320 Let's put here a hundred and say promise one. 110 00:07:58,740 --> 00:08:01,290 The promise you will be five hundred 111 00:08:03,930 --> 00:08:10,320 in the promise three will be again here is not going to be rejected. 112 00:08:10,320 --> 00:08:12,810 Actually make sure it's resolved. 113 00:08:13,050 --> 00:08:15,360 And the promise three will be how much. 114 00:08:15,880 --> 00:08:17,420 Let's put the promise three or fifty. 115 00:08:18,390 --> 00:08:21,810 So that is half of the first one and we saying it to resolve. 116 00:08:21,930 --> 00:08:26,490 You now promise that race here in passing the year. 117 00:08:27,030 --> 00:08:30,060 But I need to promise one promise 118 00:08:32,340 --> 00:08:35,580 misto and I have the promise. 119 00:08:35,580 --> 00:08:38,730 Three, Don't forget to change this because we have a different conference here. 120 00:08:39,150 --> 00:08:39,530 Right. 121 00:08:40,320 --> 00:08:46,770 And I have then data Konza lock that data or whatever you want. 122 00:08:47,400 --> 00:08:50,110 So what do you think is going to be what is going to happen? 123 00:08:50,430 --> 00:08:52,650 What is the expected output? 124 00:08:53,190 --> 00:08:53,700 OK. 125 00:08:56,300 --> 00:09:04,090 Let's refresh let's face this one here, hit enter, promised more spending and promise three. 126 00:09:04,640 --> 00:09:06,490 Well, why is this is happening? 127 00:09:06,500 --> 00:09:14,420 Why I don't go from the first, second and third promise three has been resolved, but it's 50 milliseconds 128 00:09:14,630 --> 00:09:16,270 right now. 129 00:09:17,570 --> 00:09:19,110 What is a promise that race. 130 00:09:19,150 --> 00:09:22,970 So this is actually a race between three promises to the race method returns. 131 00:09:22,970 --> 00:09:25,880 A promise that fulfills or rejects doesn't matter. 132 00:09:26,240 --> 00:09:33,920 As soon as one of the promises of this iterable here, Arae fulfills or rejects. 133 00:09:34,400 --> 00:09:41,170 So with the value, actually, with the value of this, with the value of the reason for from that promise. 134 00:09:41,480 --> 00:09:41,850 Nice. 135 00:09:42,080 --> 00:09:49,640 So this returns a promise that rejects or Fairfield's as soon as one of the promises here from this 136 00:09:49,910 --> 00:09:52,080 iterable fulfills or rejects. 137 00:09:53,000 --> 00:09:55,250 And I'm just pointing out the value. 138 00:09:56,090 --> 00:10:02,540 OK, what you think will happen if I put it here, reject, would you think that the first one will 139 00:10:02,540 --> 00:10:03,350 go first? 140 00:10:04,160 --> 00:10:04,790 Let's see. 141 00:10:05,660 --> 00:10:06,200 Save it. 142 00:10:07,220 --> 00:10:09,500 Clearing the east and enter. 143 00:10:09,720 --> 00:10:14,350 No I haven't got er undefined identifier promise. 144 00:10:14,360 --> 00:10:16,220 One has already been declared. 145 00:10:16,250 --> 00:10:18,560 Cool refresh again. 146 00:10:18,860 --> 00:10:19,870 Pasted down here. 147 00:10:19,880 --> 00:10:27,190 Nice because I have to refresh and I this new cause that needs to be fresh. 148 00:10:27,200 --> 00:10:27,550 Right. 149 00:10:28,160 --> 00:10:31,340 So Kirkorian one of the promises above promised three. 150 00:10:32,000 --> 00:10:32,780 Very good. 151 00:10:32,780 --> 00:10:35,200 But this is the first promise that rejected. 152 00:10:36,080 --> 00:10:36,510 Right. 153 00:10:36,590 --> 00:10:39,260 So it doesn't matter if it's going to reject or fulfill. 154 00:10:40,520 --> 00:10:42,810 That is the one the first going to raise. 155 00:10:42,830 --> 00:10:46,000 So that's that's going to be actually the winner of this race. 156 00:10:46,460 --> 00:10:46,810 Nice. 157 00:10:47,270 --> 00:10:50,480 So we have promises o promise. 158 00:10:51,380 --> 00:10:54,450 We get a raise and we have promised results. 159 00:10:55,220 --> 00:11:00,410 Now, promise resolve is OK, actually. 160 00:11:00,410 --> 00:11:01,430 Sorry, promise project. 161 00:11:01,430 --> 00:11:02,590 We're going to promise project. 162 00:11:02,810 --> 00:11:06,890 It's a method to return a promise object that is rejected with a given reason. 163 00:11:07,430 --> 00:11:07,850 Right. 164 00:11:08,040 --> 00:11:12,810 So the static promise reject function returns a promise that is rejected. 165 00:11:12,830 --> 00:11:15,890 This is good for debugging or catching an error. 166 00:11:15,890 --> 00:11:16,870 Selective error. 167 00:11:16,910 --> 00:11:20,640 So it is useful to make a reasons of an instance of an error. 168 00:11:20,660 --> 00:11:21,040 Nice. 169 00:11:21,710 --> 00:11:25,820 So here I'm going to say promis reject in. 170 00:11:25,820 --> 00:11:29,900 You will see what I try what I did say like a couple of seconds ago. 171 00:11:30,110 --> 00:11:34,130 So promise we need to call the class reject in here. 172 00:11:34,130 --> 00:11:42,830 I'm going to throw a new error and the description goes here, feel nice, I'm going to use that method 173 00:11:43,490 --> 00:11:45,580 and I can pass a function here. 174 00:11:45,620 --> 00:11:45,920 Right. 175 00:11:46,190 --> 00:11:48,620 So this function will not take any parameters. 176 00:11:48,620 --> 00:11:49,880 But what will happen? 177 00:11:50,450 --> 00:11:54,800 This bar will never be called. 178 00:11:55,100 --> 00:11:55,510 Right. 179 00:11:56,360 --> 00:11:58,880 I'm going to add a function, another comma here. 180 00:11:58,880 --> 00:12:00,320 I'm going to add another function. 181 00:12:00,620 --> 00:12:03,320 And actually here I'm going to catch the error. 182 00:12:03,830 --> 00:12:04,580 What's happening? 183 00:12:04,730 --> 00:12:08,410 Why so cancel the error. 184 00:12:08,720 --> 00:12:12,410 Actually, we're going to say this is promise. 185 00:12:14,240 --> 00:12:15,860 Reject it. 186 00:12:15,860 --> 00:12:18,350 I'm going to see the error note. 187 00:12:20,720 --> 00:12:28,030 So when we have a then method attached with the reject, that method will never go and be executed. 188 00:12:28,070 --> 00:12:31,700 It will go straight to the rest of the code right here. 189 00:12:31,700 --> 00:12:32,830 We are throwing an error. 190 00:12:32,840 --> 00:12:37,580 So the promised reject method returns a promise that is rejected with the given reason. 191 00:12:37,610 --> 00:12:39,940 It was the reason you just throwing up an error. 192 00:12:40,280 --> 00:12:47,450 This is good for debugging purposes or for error, catching more for anything that you want to test 193 00:12:47,450 --> 00:12:47,800 it out. 194 00:12:48,140 --> 00:12:49,580 Let's copy it here. 195 00:12:50,150 --> 00:12:51,140 Based it down there. 196 00:12:51,380 --> 00:12:52,070 Hit enter. 197 00:12:52,580 --> 00:12:53,200 Here it is. 198 00:12:53,450 --> 00:12:58,520 This is the promised reject error and field comes from the value past into this error. 199 00:12:59,390 --> 00:12:59,860 Nice. 200 00:13:00,620 --> 00:13:07,580 So every time we use reject, we know that the method will return a promise object that is rejected 201 00:13:07,670 --> 00:13:09,140 for a given reason. 202 00:13:09,980 --> 00:13:13,880 I'm going to copy the same thing same and I'm going to based it down here. 203 00:13:13,880 --> 00:13:15,250 It's going to say promise is resolved. 204 00:13:15,530 --> 00:13:16,790 Now this is the opposite. 205 00:13:17,330 --> 00:13:22,790 We need to use the syntax promise that resolve cool in here instead of error. 206 00:13:22,970 --> 00:13:24,740 You can print out our success message. 207 00:13:26,390 --> 00:13:37,970 Now we have the then function where it will take a value here and we will cancel the value. 208 00:13:38,390 --> 00:13:42,530 What you think we'll print here, the function error. 209 00:13:42,530 --> 00:13:46,850 Actually, this will never execute. 210 00:13:48,850 --> 00:13:53,560 Because this is totally opposite than this one, when we have a dinner, everything that is there is 211 00:13:53,560 --> 00:13:54,670 going to be executed. 212 00:13:55,120 --> 00:13:56,870 Now the problem is resolved. 213 00:13:56,890 --> 00:14:01,270 Again, going to return a promise object, but it's going to be resolved with the given value. 214 00:14:01,270 --> 00:14:02,290 And the value is here. 215 00:14:02,300 --> 00:14:03,700 We passing here in capacity. 216 00:14:03,700 --> 00:14:05,730 They can pass value, whatever it is. 217 00:14:06,040 --> 00:14:08,820 Now, if there is a dead method, it will go there. 218 00:14:08,830 --> 00:14:09,210 Right. 219 00:14:10,540 --> 00:14:13,810 OK, no, copy it here. 220 00:14:18,150 --> 00:14:27,750 Based on our success, again, the promised state is fulfilled and the results of the undefined right 221 00:14:28,650 --> 00:14:30,280 now, it will never go to this one. 222 00:14:30,300 --> 00:14:32,460 This is a promise project error. 223 00:14:32,520 --> 00:14:39,270 Actually, we don't need to see a promised project because we never said we're going to go there now. 224 00:14:39,280 --> 00:14:40,140 That's it. 225 00:14:40,320 --> 00:14:43,110 So, you know about all the promises. 226 00:14:43,680 --> 00:14:47,850 So we have a class for the promises and we can use one, two, three, four methods. 227 00:14:48,210 --> 00:14:48,630 Right. 228 00:14:49,290 --> 00:14:54,450 So this actually takes as an argument here on the array of promises. 229 00:14:54,870 --> 00:14:58,300 And as soon as they're fulfilled or rejected, it will go through here. 230 00:14:58,440 --> 00:15:00,190 So if they reject it, it will be stopped. 231 00:15:00,840 --> 00:15:06,780 We don't we're not going to have as a result at the end, an array of results, promises, promises 232 00:15:06,780 --> 00:15:07,340 that race. 233 00:15:08,310 --> 00:15:14,040 So we are just seeing here, OK, we have three promises, regardless of it's rejected or resolved. 234 00:15:15,000 --> 00:15:18,840 The one with these fasters fastest, it will go there. 235 00:15:18,870 --> 00:15:19,250 Right. 236 00:15:20,760 --> 00:15:26,280 And the we do have the promised reject and promised results, which are very simple and easy to understand. 237 00:15:26,580 --> 00:15:27,810 These will always reject. 238 00:15:28,560 --> 00:15:32,250 And we need to give the specify the reason it's good for debugging. 239 00:15:32,250 --> 00:15:33,810 And this will always be successful. 240 00:15:34,380 --> 00:15:41,940 And we are just going to print it out here then always this method that will go here, it will be executed. 241 00:15:41,940 --> 00:15:42,400 Right. 242 00:15:42,840 --> 00:15:47,220 So now if you have something important that you want to be executed, no matter what, she's going to 243 00:15:47,220 --> 00:15:47,760 put it here. 244 00:15:49,200 --> 00:15:49,710 That's it. 245 00:15:49,740 --> 00:15:50,880 So thank you very much. 246 00:15:51,090 --> 00:15:58,110 Next lecture, we're going to learn a lot more things and we are going to introduce new method into 247 00:15:58,520 --> 00:16:02,460 2020 that is important and connected with this. 248 00:16:02,790 --> 00:16:05,510 OK, so I will see you in the next one.