1 00:00:00,000 --> 00:00:09,375 [MUSIC] 2 00:00:09,375 --> 00:00:13,370 Hey, I'm Treasure, web developer and teacher here at Treehouse. 3 00:00:13,370 --> 00:00:16,300 Writing asynchronous code in Express often involves 4 00:00:16,300 --> 00:00:19,340 acting upon data whenever you happen to receive it. 5 00:00:19,340 --> 00:00:23,670 For example, say I want to retrieve a list of movies from a database and 6 00:00:23,670 --> 00:00:26,590 render that movie data into a template. 7 00:00:26,590 --> 00:00:30,260 Because of the asynchronous nature of working with the database, there's no 8 00:00:30,260 --> 00:00:35,280 guarantee I'll get that movie information back before Express renders the template. 9 00:00:35,280 --> 00:00:39,850 The result, an empty HTML template sent to the browser. 10 00:00:39,850 --> 00:00:42,910 To keep that from happening, you need a way to make sure you have 11 00:00:42,910 --> 00:00:46,170 all the data you need before the template is rendered. 12 00:00:46,170 --> 00:00:50,540 Traditionally, Express uses call backs to deal with asynchronous tasks. 13 00:00:50,540 --> 00:00:53,470 You'd call a function that retrieves a list of movies, and 14 00:00:53,470 --> 00:00:56,470 that function would take a call back that runs when and 15 00:00:56,470 --> 00:01:01,080 only when the list of movies has returned, or an error has occurred. 16 00:01:01,080 --> 00:01:04,310 This is fine if the actions you're performing are simple. 17 00:01:04,310 --> 00:01:08,200 It's when you have several asynchronous tasks to perform before you send something 18 00:01:08,200 --> 00:01:10,700 to the browser that things can get messy. 19 00:01:10,700 --> 00:01:15,610 For example, if you needed to retrieve a certain subset of movies from a database, 20 00:01:15,610 --> 00:01:16,462 then the ratings for 21 00:01:16,462 --> 00:01:20,140 those movies, then a list of actors who starred in those movies. 22 00:01:20,140 --> 00:01:24,740 You'd end up with a call back, nested in a call back, nested in a call back. 23 00:01:24,740 --> 00:01:26,860 We'll see an example of this in a later video. 24 00:01:28,070 --> 00:01:31,750 This is one reason that approaches to asynchronous programming in JavaScript 25 00:01:31,750 --> 00:01:34,460 have evolved and changed a lot over the years. 26 00:01:34,460 --> 00:01:39,030 Making the async landscape sometimes difficult and overwhelming to navigate. 27 00:01:39,030 --> 00:01:43,080 Recently, new approaches have become available natively in JavaScript. 28 00:01:43,080 --> 00:01:46,590 In this course we'll look more closely at two other approaches and 29 00:01:46,590 --> 00:01:50,710 how you can use them in Express, Promises and async/await. 30 00:01:50,710 --> 00:01:54,271 These approaches are used by popular libraries like Mongoose and 31 00:01:54,271 --> 00:01:57,800 Sequelize that help you interact with databases. 32 00:01:57,800 --> 00:02:01,790 As a developer, you're likely to encounter and will need to understand how these 33 00:02:01,790 --> 00:02:06,960 three approaches, callbacks, Promises, and async/await are used in Express. 34 00:02:06,960 --> 00:02:09,470 This course assumes that you're familiar with the basics 35 00:02:09,470 --> 00:02:12,530 of building a server side application with Express. 36 00:02:12,530 --> 00:02:15,400 You should understand what asynchronous programming is, 37 00:02:15,400 --> 00:02:19,460 feel comfortable with callbacks, and have some familiarity with Promises. 38 00:02:19,460 --> 00:02:22,830 If you've never heard of asynchronous programing or Promises, 39 00:02:22,830 --> 00:02:25,978 see the teacher's notes for resources that should help you get up to speed. 40 00:02:25,978 --> 00:02:30,920 While asynchronous programming can be a big and confounding topic in JavaScript, 41 00:02:30,920 --> 00:02:34,920 I hope you'll finish this workshop feeling more confident in your knowledge about how 42 00:02:34,920 --> 00:02:38,520 each of these approaches are used to build applications in Express.