1 00:00:06,690 --> 00:00:09,360 - Promises are used a lot in modern JavaScript 2 00:00:09,360 --> 00:00:11,790 and typically they call rest services 3 00:00:11,790 --> 00:00:13,320 where the result is going to take 4 00:00:13,320 --> 00:00:14,460 a few seconds to come back. 5 00:00:14,460 --> 00:00:17,760 For example, the fetch function returns a promise object. 6 00:00:17,760 --> 00:00:20,100 It returns the Promise object immediately 7 00:00:20,100 --> 00:00:22,950 and the Promise object is like an empty box. 8 00:00:22,950 --> 00:00:25,740 Eventually, the result will be placed into that box 9 00:00:25,740 --> 00:00:27,390 but not just yet. 10 00:00:27,390 --> 00:00:28,830 So what we'll do in this section 11 00:00:28,830 --> 00:00:32,430 first is to describe how promises work in a web application. 12 00:00:32,430 --> 00:00:34,350 And then what we'll do in the next section 13 00:00:34,350 --> 00:00:36,510 is see how to test that. 14 00:00:36,510 --> 00:00:38,190 For this section and the next section, 15 00:00:38,190 --> 00:00:39,990 it's going to be the same code base. 16 00:00:39,990 --> 00:00:42,783 It's the test and Promises folder in lesson four. 17 00:00:44,010 --> 00:00:46,140 Okay, so here's the basic idea. 18 00:00:46,140 --> 00:00:50,460 When you use promises, you create a promise object 19 00:00:50,460 --> 00:00:54,150 and the promise constructor calls you back immediately. 20 00:00:54,150 --> 00:00:56,790 You basically pass a parameter into the promise 21 00:00:56,790 --> 00:00:57,870 a callback function 22 00:00:57,870 --> 00:01:00,330 and the promise constructor 23 00:01:00,330 --> 00:01:03,030 invokes your callback function immediately. 24 00:01:03,030 --> 00:01:08,030 And your callback function receives two parameters, 25 00:01:09,210 --> 00:01:12,450 resolve parameter and reject parameter. 26 00:01:12,450 --> 00:01:16,530 These two parameters are pointers back into the promise. 27 00:01:16,530 --> 00:01:19,683 The idea is that inside your lambda in red here, 28 00:01:20,640 --> 00:01:22,170 you do some long running task. 29 00:01:22,170 --> 00:01:24,390 It might take a few seconds. 30 00:01:24,390 --> 00:01:27,870 If that succeeds, then you call the resolve function 31 00:01:27,870 --> 00:01:30,480 whatever this points to in the promise object 32 00:01:30,480 --> 00:01:33,510 you basically call the resolve function pointer 33 00:01:33,510 --> 00:01:37,230 to tell the promise I have succeeded and choose the result. 34 00:01:37,230 --> 00:01:38,643 It's probably gonna be 42. 35 00:01:39,810 --> 00:01:41,460 If anything went wrong, 36 00:01:41,460 --> 00:01:44,310 then you call the reject call back on the promise 37 00:01:44,310 --> 00:01:46,060 and you give it some kind of error. 38 00:01:46,980 --> 00:01:47,813 Okay? 39 00:01:47,813 --> 00:01:49,710 So there's my long running task. 40 00:01:49,710 --> 00:01:51,960 If the long running task is successful 41 00:01:51,960 --> 00:01:53,673 we invoke the resolve call back. 42 00:01:54,510 --> 00:01:57,270 If it's unsuccessful, we invoke the reject call back. 43 00:01:57,270 --> 00:02:00,510 So the promise object will know at the end of the day 44 00:02:00,510 --> 00:02:03,213 what the result was or what the error was. 45 00:02:04,770 --> 00:02:06,330 In terms of using the promise object 46 00:02:06,330 --> 00:02:07,890 back in your client code 47 00:02:07,890 --> 00:02:10,510 you'd say, having created the promise object 48 00:02:12,090 --> 00:02:14,220 you can say, when that has finished 49 00:02:14,220 --> 00:02:17,700 if it finished successfully by a resolution 50 00:02:17,700 --> 00:02:21,630 then this is the code that I want to execute. 51 00:02:21,630 --> 00:02:22,463 Okay? 52 00:02:22,463 --> 00:02:24,120 So when the promise object is resolved 53 00:02:24,120 --> 00:02:25,560 it'll call you back 54 00:02:25,560 --> 00:02:27,480 it'll call this lambda 55 00:02:27,480 --> 00:02:30,240 it'll pass the result that was returned 56 00:02:30,240 --> 00:02:32,010 and you can do something with it. 57 00:02:32,010 --> 00:02:35,370 On the other hand, if the promise object rejected 58 00:02:35,370 --> 00:02:37,530 then it'll call your catch callback. 59 00:02:37,530 --> 00:02:39,300 And then here you can have some error handling 60 00:02:39,300 --> 00:02:41,340 you'll receive the error parameter, 61 00:02:41,340 --> 00:02:42,510 and you can do something with it, 62 00:02:42,510 --> 00:02:45,063 like display a popup message on the console. 63 00:02:46,080 --> 00:02:47,850 There's also an alternative syntax. 64 00:02:47,850 --> 00:02:52,200 Instead of having a promise dot then do this 65 00:02:52,200 --> 00:02:54,540 dot catch, do that 66 00:02:54,540 --> 00:02:56,400 You can write it slightly differently. 67 00:02:56,400 --> 00:02:58,980 There's a then function on promise 68 00:02:58,980 --> 00:03:00,480 where it takes two parameters, 69 00:03:00,480 --> 00:03:01,830 two lambdas effectively. 70 00:03:01,830 --> 00:03:04,500 The first function is what to do 71 00:03:04,500 --> 00:03:06,750 if the promise was resolved successfully. 72 00:03:06,750 --> 00:03:08,730 And the second parameter is what to do 73 00:03:08,730 --> 00:03:10,710 if the promise was rejected. 74 00:03:10,710 --> 00:03:11,543 Okay? 75 00:03:11,543 --> 00:03:13,080 So, semantically the same idea 76 00:03:13,080 --> 00:03:15,270 just a different way of writing the code. 77 00:03:15,270 --> 00:03:16,103 And if you want to 78 00:03:16,103 --> 00:03:19,080 you can actually set up a chain of promises. 79 00:03:19,080 --> 00:03:21,990 So you can say, right, get that promise running 80 00:03:21,990 --> 00:03:25,920 when it's finished, if it works successfully, then do this. 81 00:03:25,920 --> 00:03:28,860 And in here you might have some other long running task. 82 00:03:28,860 --> 00:03:30,420 And when that's finished do this 83 00:03:30,420 --> 00:03:33,450 and each time, the result of the previous step 84 00:03:33,450 --> 00:03:35,490 gets passed in as the result in here. 85 00:03:35,490 --> 00:03:38,850 So take the next result and do some more work 86 00:03:38,850 --> 00:03:40,560 that would give me back the next result, 87 00:03:40,560 --> 00:03:42,930 pass it into here and do some more work 88 00:03:42,930 --> 00:03:44,610 that would generate another result 89 00:03:44,610 --> 00:03:46,830 Pass that into here and do some more work, 90 00:03:46,830 --> 00:03:48,840 oh, by the way if anything failed, 91 00:03:48,840 --> 00:03:51,060 then call a catch handler here. 92 00:03:51,060 --> 00:03:56,060 In case you can set up a whole pipeline of work to do 93 00:03:56,280 --> 00:03:59,220 one after the other as each promise gets resolved, 94 00:03:59,220 --> 00:04:00,393 go onto the next step. 95 00:04:01,890 --> 00:04:03,150 One of the main benefits 96 00:04:03,150 --> 00:04:06,690 of using promises is how easy it is to set up a chain. 97 00:04:06,690 --> 00:04:08,430 It is much easier to set up a chain 98 00:04:08,430 --> 00:04:10,320 of subsequent steps like this 99 00:04:10,320 --> 00:04:12,330 than it would've been using callbacks. 100 00:04:12,330 --> 00:04:15,660 With callbacks you have a nested handler 101 00:04:15,660 --> 00:04:17,760 and the thing gets very deeply nested 102 00:04:17,760 --> 00:04:20,493 instead of being more sequential like it is here. 103 00:04:22,080 --> 00:04:25,770 Okay, so that's the general idea of promises 104 00:04:25,770 --> 00:04:29,280 to understand promises properly, an example. 105 00:04:29,280 --> 00:04:31,410 So in the test and promises folder 106 00:04:31,410 --> 00:04:33,960 I've got a webpage, index.html 107 00:04:33,960 --> 00:04:36,030 which I'm gonna open in browser. 108 00:04:36,030 --> 00:04:38,794 So test and promises is my folder 109 00:04:38,794 --> 00:04:41,550 and I'm gonna open my index HTML 110 00:04:41,550 --> 00:04:44,010 which has a style sheet associated with it. 111 00:04:44,010 --> 00:04:45,000 And what it does 112 00:04:45,000 --> 00:04:49,860 it allows the user to initiate a long running task 113 00:04:49,860 --> 00:04:51,570 and to display the results. 114 00:04:51,570 --> 00:04:54,510 This long running task will take a few seconds 115 00:04:54,510 --> 00:04:56,820 and allow the resolve or reject 116 00:04:56,820 --> 00:05:01,380 and display a message on the on the webpage. 117 00:05:01,380 --> 00:05:02,850 So when I click the button 118 00:05:02,850 --> 00:05:07,110 there's an on click handler, which runs in code. 119 00:05:07,110 --> 00:05:10,350 It generates a random number between zero and one 120 00:05:10,350 --> 00:05:14,850 and it sets up a timer based on that random number. 121 00:05:14,850 --> 00:05:18,450 If it's a bigger random number, like 0.9 122 00:05:18,450 --> 00:05:20,460 then it gives me a longer delay. 123 00:05:20,460 --> 00:05:23,220 And if it's a smaller random number, like 0.1 124 00:05:23,220 --> 00:05:24,510 it gives me a shorter delay. 125 00:05:24,510 --> 00:05:28,650 So it's like a semi random delay on the timer. 126 00:05:28,650 --> 00:05:30,960 After the timer has finished in my code 127 00:05:30,960 --> 00:05:33,810 I decide whether to resolve or reject. 128 00:05:33,810 --> 00:05:34,950 Okay? 129 00:05:34,950 --> 00:05:36,240 What I've actually got is 130 00:05:36,240 --> 00:05:37,380 I've got some hard coded. 131 00:05:37,380 --> 00:05:39,630 It's like a sample really 132 00:05:39,630 --> 00:05:42,270 dependent on whether the random number is 133 00:05:42,270 --> 00:05:44,790 greater than or less 0.5. 134 00:05:44,790 --> 00:05:47,197 I decide why to either resolve the promise 135 00:05:47,197 --> 00:05:50,280 or reject it based on the random number. 136 00:05:50,280 --> 00:05:53,790 And then I display a message in the window to say, 137 00:05:53,790 --> 00:05:56,913 oh, that particular task was resolved successfully. 138 00:05:57,780 --> 00:06:00,183 So let me show you that. 139 00:06:01,260 --> 00:06:03,630 So I'll start the task. 140 00:06:03,630 --> 00:06:04,530 So the way it works, 141 00:06:04,530 --> 00:06:08,340 if the random number is less than no 0.5 in my code, 142 00:06:08,340 --> 00:06:10,920 then I resolve the promise. 143 00:06:10,920 --> 00:06:12,603 I'll start another task. 144 00:06:13,530 --> 00:06:14,790 So that was less than no 0.5, 145 00:06:14,790 --> 00:06:16,410 that one resolved as well. 146 00:06:16,410 --> 00:06:18,150 That one was gonna fail 147 00:06:18,150 --> 00:06:21,524 because the random number was greater than, 0.5 in my code. 148 00:06:21,524 --> 00:06:25,350 In my example, it rejected the task. 149 00:06:25,350 --> 00:06:27,600 Also didn't notice the timer was a bit longer 150 00:06:27,600 --> 00:06:29,400 because the number was bigger. 151 00:06:29,400 --> 00:06:30,483 I start another one. 152 00:06:31,350 --> 00:06:32,183 Okay? 153 00:06:32,183 --> 00:06:34,860 I'll start another one, that one was gonna pass. 154 00:06:34,860 --> 00:06:37,680 I'll start another one, that one was gonna pass. 155 00:06:37,680 --> 00:06:39,900 Another one that one was gonna fail 156 00:06:39,900 --> 00:06:42,003 because the number is greater than 0.5. 157 00:06:43,110 --> 00:06:43,943 Okay? 158 00:06:43,943 --> 00:06:45,150 So it's a simple example just to 159 00:06:45,150 --> 00:06:48,088 kind of explain the syntax of promises. 160 00:06:48,088 --> 00:06:49,200 Well, there you go. 161 00:06:49,200 --> 00:06:50,033 What I could do 162 00:06:50,033 --> 00:06:52,780 is I could click the button several times in succession. 163 00:06:54,000 --> 00:06:55,353 When you create a promise, 164 00:06:57,060 --> 00:06:58,560 it does some long running task 165 00:06:58,560 --> 00:07:00,270 but the code returns immediately. 166 00:07:00,270 --> 00:07:02,880 So you can click the button again and again and again. 167 00:07:02,880 --> 00:07:06,390 So I can click the button several times in rapid succession. 168 00:07:06,390 --> 00:07:08,700 Every time the on click handle occurs 169 00:07:08,700 --> 00:07:10,950 it creates a new promise object. 170 00:07:10,950 --> 00:07:13,980 The promise will in the background, 171 00:07:13,980 --> 00:07:18,540 resolve or reject and display a message on the console. 172 00:07:18,540 --> 00:07:20,610 Every time I create a new promise 173 00:07:20,610 --> 00:07:22,770 I've got like an on click count. 174 00:07:22,770 --> 00:07:24,750 It'll know that this is promise one 175 00:07:24,750 --> 00:07:26,160 that's promise two, 176 00:07:26,160 --> 00:07:27,360 that's promise three, 177 00:07:27,360 --> 00:07:31,440 the promises will get resolved potentially out of order. 178 00:07:31,440 --> 00:07:33,570 The longer a promise takes to run 179 00:07:33,570 --> 00:07:36,600 the longer it'll be before it's resolved or rejected 180 00:07:36,600 --> 00:07:39,960 another promise might resolve or reject sooner. 181 00:07:39,960 --> 00:07:42,336 So you can see here, in this example, 182 00:07:42,336 --> 00:07:44,370 I created five tasks, 183 00:07:44,370 --> 00:07:46,020 but they resolved in a different order 184 00:07:46,020 --> 00:07:49,230 based on how long the time out was for each one. 185 00:07:49,230 --> 00:07:51,780 Task one was quite a long time out. 186 00:07:51,780 --> 00:07:54,390 So that took a long time to fail actually 187 00:07:54,390 --> 00:07:57,090 task two was also quite a long time out 188 00:07:57,090 --> 00:07:58,080 but not as long. 189 00:07:58,080 --> 00:08:00,510 So task two completed before task one 190 00:08:00,510 --> 00:08:02,110 because the numbers are smaller. 191 00:08:03,270 --> 00:08:05,490 Task four was the first one to complete 192 00:08:05,490 --> 00:08:07,590 because it had a very small random number. 193 00:08:08,460 --> 00:08:11,190 Okay, so I'm gonna do that just to prove the point. 194 00:08:11,190 --> 00:08:13,200 Up here, let me just refresh the window. 195 00:08:13,200 --> 00:08:16,984 And I'll click the button a few times in rapid succession. 196 00:08:16,984 --> 00:08:19,680 (button clicking) 197 00:08:19,680 --> 00:08:20,513 Okay? 198 00:08:20,513 --> 00:08:22,920 So you can see, depending on whether the random number 199 00:08:22,920 --> 00:08:26,370 is greater than, or less than no 0.5 in my example code, 200 00:08:26,370 --> 00:08:28,770 it'd like cues to resolve or reject 201 00:08:28,770 --> 00:08:32,100 and the bigger the number the longer the delay. 202 00:08:32,100 --> 00:08:32,933 Okay? 203 00:08:32,933 --> 00:08:33,766 Very pretty. 204 00:08:33,766 --> 00:08:34,920 Let's have a look at the code. 205 00:08:34,920 --> 00:08:36,360 First of all, 206 00:08:36,360 --> 00:08:39,660 I've got a code that performs a long running task 207 00:08:39,660 --> 00:08:41,700 and returns a promise object. 208 00:08:41,700 --> 00:08:45,813 So that code is in task dot js, task.js. 209 00:08:47,370 --> 00:08:50,310 Okay, so this is my long running task. 210 00:08:50,310 --> 00:08:54,210 It takes a task number, 1, 2, 3, 211 00:08:54,210 --> 00:08:55,950 and the random number, 212 00:08:55,950 --> 00:08:58,740 which will govern whether it succeeds or fails. 213 00:08:58,740 --> 00:09:01,140 So it creates a promise object. 214 00:09:01,140 --> 00:09:04,830 And in that promise, I give it a piece of work. 215 00:09:04,830 --> 00:09:06,750 Whenever you create a promise object 216 00:09:06,750 --> 00:09:09,570 the constructor for promise always takes a function. 217 00:09:09,570 --> 00:09:11,373 It calls that function immediately. 218 00:09:12,240 --> 00:09:13,073 Okay? 219 00:09:13,073 --> 00:09:15,480 And then returns that promise object. 220 00:09:15,480 --> 00:09:17,430 That promise object gets returned immediately 221 00:09:17,430 --> 00:09:19,263 back to my user interface. 222 00:09:20,190 --> 00:09:25,050 Inside here, I've got a long running task. 223 00:09:25,050 --> 00:09:30,050 So after a random portion of five seconds, 224 00:09:30,630 --> 00:09:33,600 the bigger the random number, the bigger the delay 225 00:09:33,600 --> 00:09:35,100 after that delay, 226 00:09:35,100 --> 00:09:37,800 it will invoke the code here in my set timeout. 227 00:09:37,800 --> 00:09:41,400 So that's where the asynchronous nature comes in 228 00:09:41,400 --> 00:09:43,260 and where the delay comes in. 229 00:09:43,260 --> 00:09:48,260 So after a random amount of time, it will decide what to do. 230 00:09:48,330 --> 00:09:49,163 And it says, well, okay, 231 00:09:49,163 --> 00:09:51,720 if the number was less than 0.5, 232 00:09:51,720 --> 00:09:53,970 I will tell the promise to resolve. 233 00:09:53,970 --> 00:09:58,350 Resolve is a pointer back to a function inside the promise 234 00:09:58,350 --> 00:10:02,100 to say, yes, it worked and here is the result. 235 00:10:02,100 --> 00:10:03,930 When you call the resolve function, 236 00:10:03,930 --> 00:10:05,370 whatever you pass in as a prompter, 237 00:10:05,370 --> 00:10:07,410 that's the success result 238 00:10:07,410 --> 00:10:09,960 that the promise object will report back. 239 00:10:09,960 --> 00:10:13,060 So it'll say something like, task one resolved 240 00:10:14,070 --> 00:10:15,420 or alternatively, 241 00:10:15,420 --> 00:10:18,250 if the random number was 0.5 or above 242 00:10:19,110 --> 00:10:21,390 then the promise will reject. 243 00:10:21,390 --> 00:10:22,223 Okay? 244 00:10:22,223 --> 00:10:23,640 So, this is my function 245 00:10:23,640 --> 00:10:26,790 It will execute some code in the background. 246 00:10:26,790 --> 00:10:28,380 The promise object will either, 247 00:10:28,380 --> 00:10:31,113 eventually be resolved or rejected. 248 00:10:32,370 --> 00:10:33,203 Okay. 249 00:10:33,203 --> 00:10:35,160 So that's my work code. 250 00:10:35,160 --> 00:10:39,120 I've got another file, which has my user interface code. 251 00:10:39,120 --> 00:10:40,890 So let's have a look at that next. 252 00:10:40,890 --> 00:10:41,790 So that's UI case. 253 00:10:44,490 --> 00:10:46,140 So as you would imagine 254 00:10:46,140 --> 00:10:47,280 while I've got a promise count, 255 00:10:47,280 --> 00:10:50,160 that will be incremented every time we do a button click 256 00:10:50,160 --> 00:10:52,260 this is my on click handler. 257 00:10:52,260 --> 00:10:53,460 If you looked at the webpage, 258 00:10:53,460 --> 00:10:56,460 it calls this function whenever I click the button, 259 00:10:56,460 --> 00:10:58,980 increment the promise count 260 00:10:58,980 --> 00:11:03,243 and generate a random number for this particular task. 261 00:11:04,110 --> 00:11:07,293 Oh, it displays a message on the webpage. 262 00:11:08,550 --> 00:11:12,630 In my CSS, I've got some CSS classes. 263 00:11:12,630 --> 00:11:14,640 Let me just quickly show you that. 264 00:11:14,640 --> 00:11:19,590 In my style CSS, I've got a CSS class of info 265 00:11:19,590 --> 00:11:22,020 which displays the blue messages. 266 00:11:22,020 --> 00:11:24,600 Those are the informational messages there. 267 00:11:24,600 --> 00:11:26,910 I've got a class called resolved, 268 00:11:26,910 --> 00:11:29,430 which will display the green messages. 269 00:11:29,430 --> 00:11:31,530 If a promise is resolved. 270 00:11:31,530 --> 00:11:34,080 And I've got a rejected CSS class, 271 00:11:34,080 --> 00:11:38,070 which I apply if the promise is rejected. 272 00:11:38,070 --> 00:11:40,650 And obviously those are these ones done here. 273 00:11:40,650 --> 00:11:41,483 Okay. 274 00:11:41,483 --> 00:11:44,580 So in my use of interface code, first of all 275 00:11:44,580 --> 00:11:47,400 in the display message function, 276 00:11:47,400 --> 00:11:49,960 it takes the name of a CSS class and a message 277 00:11:51,180 --> 00:11:52,440 gets that message area, 278 00:11:52,440 --> 00:11:54,990 which is a div at the bottom of the page. 279 00:11:54,990 --> 00:11:58,650 And it adds a chunk of HTML into that div. 280 00:11:58,650 --> 00:12:01,650 It basically displays another message in the panel down here 281 00:12:02,700 --> 00:12:04,680 in the class that I have specified, 282 00:12:04,680 --> 00:12:06,810 the CSS class I have specified, 283 00:12:06,810 --> 00:12:11,810 like info or resolved or rejected 284 00:12:11,940 --> 00:12:13,170 that will decide whether 285 00:12:13,170 --> 00:12:17,280 the message is displayed in blue or red or green, 286 00:12:17,280 --> 00:12:18,813 and then the message itself. 287 00:12:19,860 --> 00:12:20,693 Okay? 288 00:12:20,693 --> 00:12:22,740 So when I call the display message here 289 00:12:22,740 --> 00:12:26,823 it'll pass in the info class to display in blue. 290 00:12:27,780 --> 00:12:28,680 And the message is, 291 00:12:28,680 --> 00:12:33,680 task one about to start with random number X. 292 00:12:33,900 --> 00:12:37,230 And that's the message that we saw being displayed here. 293 00:12:37,230 --> 00:12:38,063 Okay? 294 00:12:38,063 --> 00:12:39,090 And then it went off 295 00:12:39,090 --> 00:12:42,420 and basically kicked off that asynchronous work 296 00:12:42,420 --> 00:12:45,573 that function returns immediately with the promise object. 297 00:12:46,699 --> 00:12:47,532 Okay? 298 00:12:47,532 --> 00:12:50,280 That promise object will eventually complete 299 00:12:50,280 --> 00:12:52,350 one way or the other. 300 00:12:52,350 --> 00:12:56,490 If it succeeds, you know, five seconds later 301 00:12:56,490 --> 00:13:01,490 then the value that it returns will be a message like this 302 00:13:01,860 --> 00:13:05,010 task X resolved. 303 00:13:05,010 --> 00:13:06,300 So it'll take that value 304 00:13:06,300 --> 00:13:09,900 and it'll display that with the resolved class 305 00:13:09,900 --> 00:13:11,500 to be displayed in green 306 00:13:12,630 --> 00:13:16,710 or alternatively, if my promise was rejected 307 00:13:16,710 --> 00:13:19,800 because the number was too large 308 00:13:19,800 --> 00:13:23,760 then the error message that accompanied the rejection 309 00:13:23,760 --> 00:13:27,423 that error message comes into my cat handler. 310 00:13:28,350 --> 00:13:29,580 And that error message, 311 00:13:29,580 --> 00:13:31,923 I then display with the red font, 312 00:13:33,330 --> 00:13:34,920 like so. 313 00:13:34,920 --> 00:13:35,753 Okay? 314 00:13:35,753 --> 00:13:37,980 So we've seen how to create a promise. 315 00:13:37,980 --> 00:13:39,690 You create a promise 316 00:13:39,690 --> 00:13:42,900 and you have a long function 317 00:13:42,900 --> 00:13:45,030 in that long running function 318 00:13:45,030 --> 00:13:45,900 when it's finished, 319 00:13:45,900 --> 00:13:47,430 you either tell the promise that 320 00:13:47,430 --> 00:13:50,370 it's been resolved with the result 321 00:13:50,370 --> 00:13:53,580 or rejected with an error message. 322 00:13:53,580 --> 00:13:57,090 And then the code that creates the promise can say, 323 00:13:57,090 --> 00:13:59,090 okay that promise that I've just created 324 00:14:00,060 --> 00:14:02,820 if it completes then do this, 325 00:14:02,820 --> 00:14:05,550 if it fails, then do that. 326 00:14:05,550 --> 00:14:06,390 Okay, so hopefully 327 00:14:06,390 --> 00:14:09,930 by now we have a fairly decent grip of how promises work. 328 00:14:09,930 --> 00:14:11,550 What we'll do in the next section 329 00:14:11,550 --> 00:14:14,400 is see how to test that promise code 330 00:14:14,400 --> 00:14:17,433 both for successful completion and for rejection.