1 00:00:00,000 --> 00:00:04,308 [MUSIC] 2 00:00:04,308 --> 00:00:07,824 Promises in JavaScript offer a more elegant and human readable 3 00:00:07,824 --> 00:00:12,265 way to manage asynchronous code without having to write too many callbacks, or 4 00:00:12,265 --> 00:00:16,140 spend extra time figuring out what the program should do. 5 00:00:16,140 --> 00:00:20,320 Promises also give you more control over your asynch programs. 6 00:00:20,320 --> 00:00:21,480 What is a promise? 7 00:00:21,480 --> 00:00:26,770 Well a promise represents the eventual completion of a asynchronous operation. 8 00:00:26,770 --> 00:00:27,370 In other words, 9 00:00:27,370 --> 00:00:31,790 a value that may not be available yet, like data requested from a server. 10 00:00:31,790 --> 00:00:34,830 So let's spend a little bit of time unpacking what all this means 11 00:00:34,830 --> 00:00:39,090 by first looking at the differences between callbacks and promises. 12 00:00:39,090 --> 00:00:39,720 Let's say that, 13 00:00:39,720 --> 00:00:43,690 instead of making breakfast yourself, you're in the mood to order breakfast. 14 00:00:43,690 --> 00:00:45,920 There are two ways you might order breakfast. 15 00:00:45,920 --> 00:00:48,480 You can order by phone and have it delivered, or 16 00:00:48,480 --> 00:00:51,720 you can order at the restaurant and pick it up when ready. 17 00:00:51,720 --> 00:00:55,050 In the first scenario, you call your favorite breakfast spot and 18 00:00:55,050 --> 00:00:58,800 place your order, providing them your phone number and address. 19 00:00:58,800 --> 00:01:00,660 The order taker says okay, thanks. 20 00:01:00,660 --> 00:01:02,010 I'll put in your order now and 21 00:01:02,010 --> 00:01:04,740 call you back to let you know when the order is ready and on its way. 22 00:01:04,740 --> 00:01:05,580 And that's it. 23 00:01:05,580 --> 00:01:07,610 At this point, you can go on and 24 00:01:07,610 --> 00:01:10,700 do other things while breakfast gets there, that's convenient. 25 00:01:10,700 --> 00:01:14,200 But you're also left waiting for a call back from the restaurant. 26 00:01:14,200 --> 00:01:17,340 You really don't have control over when they're going to call you. 27 00:01:17,340 --> 00:01:19,180 You received nothing in exchange. 28 00:01:19,180 --> 00:01:21,930 For example, a ticket number to confirm your order. 29 00:01:21,930 --> 00:01:23,240 You just wait for them to call and 30 00:01:23,240 --> 00:01:25,630 deliver your food at around the time they said they would. 31 00:01:26,730 --> 00:01:29,404 In this scenario, you've handed control over whether or 32 00:01:29,404 --> 00:01:32,690 not you will have breakfast to the person taking your order. 33 00:01:32,690 --> 00:01:35,930 You expect it to happen, but maybe they wrote down the wrong phone number, and 34 00:01:35,930 --> 00:01:39,510 they can't call back or they call, but your food never makes it to you. 35 00:01:39,510 --> 00:01:41,890 Perhaps they even deliver the wrong order. 36 00:01:41,890 --> 00:01:44,650 Now, most of these problems could be eventually rectified, but 37 00:01:44,650 --> 00:01:45,940 it's a messy process. 38 00:01:45,940 --> 00:01:50,050 A process which hands the control back to you when, and only when, 39 00:01:50,050 --> 00:01:51,630 breakfast is delivered successfully. 40 00:01:52,760 --> 00:01:55,380 This is similar to working with callback functions. 41 00:01:55,380 --> 00:01:59,320 You're in some ways handing over control of your function to a callback 42 00:01:59,320 --> 00:02:01,732 expecting something else to be called and executed. 43 00:02:01,732 --> 00:02:06,020 Like callbacks, the process could be more susceptible to errors and 44 00:02:06,020 --> 00:02:08,470 harder to debug if something goes wrong. 45 00:02:08,470 --> 00:02:12,400 In the second scenario, you go to the breakfast place, put in your order and 46 00:02:12,400 --> 00:02:13,390 pay for it. 47 00:02:13,390 --> 00:02:17,880 The cashier then hands you something back in return, a receipt with an order number 48 00:02:17,880 --> 00:02:21,290 and a pager that buzzes to let you know that your order is ready. 49 00:02:21,290 --> 00:02:22,670 At that point, you may go off and 50 00:02:22,670 --> 00:02:27,130 do other things while you wait, like grab a coffee next door or catch up on email. 51 00:02:27,130 --> 00:02:29,650 You're still waiting for the pager to buzz. 52 00:02:29,650 --> 00:02:34,250 But by just having the pager in hand, you now have more control over whether or 53 00:02:34,250 --> 00:02:35,660 not you will have breakfast. 54 00:02:35,660 --> 00:02:40,170 The pager represents a promise that something is going to happen as a result 55 00:02:40,170 --> 00:02:42,110 of ordering breakfast. 56 00:02:42,110 --> 00:02:45,910 With that being said, the promise is pending because you're waiting, so 57 00:02:45,910 --> 00:02:47,610 one of two things could happen. 58 00:02:47,610 --> 00:02:50,540 The pager buzzes, your order becomes available to you, 59 00:02:50,540 --> 00:02:55,230 so you pick it up and breakfast is fulfilled, or the pager buzzes but 60 00:02:55,230 --> 00:02:57,110 not because your order is ready. 61 00:02:57,110 --> 00:03:00,130 Instead, it's to inform you that your food cannot be made and 62 00:03:00,130 --> 00:03:02,750 the reason why your order was in some way rejected. 63 00:03:03,810 --> 00:03:07,870 This second scenario happens to be more like how promises in JavaScript work. 64 00:03:07,870 --> 00:03:11,500 A promise, like a pager, represents a value that can be 65 00:03:11,500 --> 00:03:14,940 handled at some point later, like picking up your order. 66 00:03:14,940 --> 00:03:18,960 While a callback may leave us with some uncertainties about that value, 67 00:03:18,960 --> 00:03:23,960 a promise will promise or guarantee a future value and nothing can change it, 68 00:03:23,960 --> 00:03:27,019 regardless of whether or not it comes back fulfilled or rejected. 69 00:03:28,320 --> 00:03:32,170 Up next, you will learn how to create a promise, then we'll continue with 70 00:03:32,170 --> 00:03:35,670 our people in space project by converting the callback functions to promises.