1 00:00:00,475 --> 00:00:05,283 Now you're going to learn how to create a promise and get a result out of a promise, 2 00:00:05,283 --> 00:00:07,530 then how to chain promises together. 3 00:00:07,530 --> 00:00:11,809 A promise is always going to be in one of three possible states, 4 00:00:11,809 --> 00:00:14,408 pending, fulfilled, or rejected. 5 00:00:14,408 --> 00:00:18,202 The pending state is like the waiting state you're in after being handed 6 00:00:18,202 --> 00:00:18,832 the pager. 7 00:00:18,832 --> 00:00:21,160 It's the default state of a promise. 8 00:00:21,160 --> 00:00:24,956 Fulfilled means that the operation completed successfully. 9 00:00:24,956 --> 00:00:28,180 And rejected means that the operation failed. 10 00:00:28,180 --> 00:00:31,264 Working with promises is a three step sequence. 11 00:00:31,264 --> 00:00:35,429 You create a promise instance using the Promise() constructor. 12 00:00:35,429 --> 00:00:40,283 You then define what should happen when the promise is fulfilled or rejected. 13 00:00:40,283 --> 00:00:44,966 Then call one of the methods provided by the promise API to either consume 14 00:00:44,966 --> 00:00:49,342 the value of a fulfilled promise, or provide a rejection reason for 15 00:00:49,342 --> 00:00:50,751 a rejected promise. 16 00:00:50,751 --> 00:00:52,591 Let's see how all of this works. 17 00:00:52,591 --> 00:00:56,206 In the project, open the file promises-breakfast.js. 18 00:00:56,206 --> 00:01:01,021 In this file, let's create a new promise called breakfast promise 19 00:01:01,021 --> 00:01:03,309 with const breakfastPromise. 20 00:01:03,309 --> 00:01:05,652 We create the promise using the new keyword, 21 00:01:05,652 --> 00:01:08,432 followed by the Promise() constructor function. 22 00:01:10,473 --> 00:01:14,822 The Promise() constructor takes one argument, 23 00:01:14,822 --> 00:01:18,664 a callback with two perimeters, resolve for 24 00:01:18,664 --> 00:01:24,543 when the promise is fulfilled, and reject for when it's rejected. 25 00:01:24,543 --> 00:01:30,358 Within the callback, I'll write an async task using the setTimeout method. 26 00:01:30,358 --> 00:01:38,623 I'll pass it a callback and set a delay value of 3,000 milliseconds, or 3 seconds. 27 00:01:38,623 --> 00:01:41,602 Our Promise() constructor function is all set up. 28 00:01:41,602 --> 00:01:46,781 This function is going to return a promise object that is either pending, 29 00:01:46,781 --> 00:01:48,627 resolved, or rejected. 30 00:01:48,627 --> 00:01:53,137 For example, if I console.log(breakfastPromise), 31 00:01:53,137 --> 00:01:58,750 then in the terminal run this file with node promises-breakfast.js, 32 00:01:58,750 --> 00:02:04,925 notice how the console outputs a promise object, which is currently pending. 33 00:02:04,925 --> 00:02:09,154 And that's correct because pending is the default state of a promise. 34 00:02:09,154 --> 00:02:13,797 So now, inside the constructor, we can either call resolve to 35 00:02:13,797 --> 00:02:18,438 return a promise object that is resolved with a given value, or 36 00:02:18,438 --> 00:02:23,713 reject to return a promise object that is rejected with a given reason. 37 00:02:23,713 --> 00:02:26,385 Both of these functions are provided by the constructor. 38 00:02:26,385 --> 00:02:31,052 So first, inside setTimeout, I'll call resolve. 39 00:02:31,052 --> 00:02:33,749 And resolve does just that, it changes or 40 00:02:33,749 --> 00:02:37,842 resolves the status of the promise from pending to fulfilled. 41 00:02:37,842 --> 00:02:42,242 We can pass resolve a value which then becomes the fulfillment value of 42 00:02:42,242 --> 00:02:43,146 the promise. 43 00:02:43,146 --> 00:02:51,518 For example, I'll pass resolve the string Your order is ready, come and get it! 44 00:02:51,518 --> 00:02:56,556 So when the asynchronous task completes successfully, it's going to return 45 00:02:56,556 --> 00:03:01,758 a resolved promise object with Your order is ready, come and get it as its value. 46 00:03:01,758 --> 00:03:04,019 Running the file again in the console, 47 00:03:04,019 --> 00:03:06,629 we see that the promise is in a pending state. 48 00:03:06,629 --> 00:03:11,888 So now we need to get the value out of the promise object, or consume the promise. 49 00:03:11,888 --> 00:03:16,846 To do that, the Promise API offers methods you can call on the promise. 50 00:03:16,846 --> 00:03:21,368 The two you'll likely use most are then() and catch(). 51 00:03:21,368 --> 00:03:26,359 So first, I'll call then() on our breakfast 52 00:03:26,359 --> 00:03:30,735 promise with breakfastPromise.then. 53 00:03:30,735 --> 00:03:35,669 then() can be called to handle both fulfilled and rejected promises. 54 00:03:35,669 --> 00:03:40,130 It accepts two functions as its argument, one for fulfilled promises, and 55 00:03:40,130 --> 00:03:42,510 an optional one for rejected promises. 56 00:03:42,510 --> 00:03:45,949 In this case, I'm only calling it to handle fulfilled promises, and 57 00:03:45,949 --> 00:03:48,766 later use the catch method to handle rejected promises. 58 00:03:48,766 --> 00:03:54,053 So to get the resolved value, I'll pass then() an arrow function that takes 59 00:03:54,053 --> 00:03:59,514 the fulfillment value as a parameter I'll name val, and logs it to the console. 60 00:04:03,467 --> 00:04:06,383 Now I'll run this file again in the console, 61 00:04:06,383 --> 00:04:08,694 we see that the promise is pending. 62 00:04:08,694 --> 00:04:13,660 Then after three seconds, it resolves to the value Your order is ready, come and 63 00:04:13,660 --> 00:04:14,183 get it. 64 00:04:14,183 --> 00:04:21,043 Good, so after the three second delay, the promise changes from pending to fulfilled. 65 00:04:21,043 --> 00:04:25,561 Then the function passed to then() is immediately invoked, 66 00:04:25,561 --> 00:04:28,384 which returns the fulfillment value. 67 00:04:28,384 --> 00:04:33,106 In other words, whatever we pass the resolve method is 68 00:04:33,106 --> 00:04:36,504 available to be used here inside then. 69 00:04:36,504 --> 00:04:39,294 Not all promises are resolved, so in the next video, 70 00:04:39,294 --> 00:04:43,270 you'll learn how to handle rejected promises using the catch() method.