 Welcome back. Do you want to know how to wrap an existing API in promises? How about running multiple promises at the same time? I'm Sarah Clark, and it's time to master promises. Remember what a promise does. It wraps asynchronous code so you can use it more easily. It needs to wrap both callback-based and event-based code. So with that requirement in mind, let's look at an example. We're going to write a custom function that wraps XML HTTP request in a promise. We'll call this function xhr. Then we can use it in a similar way to fetch. Here's the template for creating a promise. The promise constructor takes a function and runs it. That function, in turn, takes one or more callbacks that report the inner code's status. In this example, our function has two callbacks named resolve and reject. When the asynchronous function completes, you can call one of these to communicate the result. We can start our xhr function by creating a new promise and setting up the inner function. You may remember how this API works. We need to create a request object, attach some event listeners, then populate and send the request. Notice that we've highlighted the load event handler. We need to add this code to mark the promise as resolved. When the load completes, we can resolve the promise and pass the result. Any function registered with thought then will receive this result. What about error handling? An error should call the reject callback and pass in the exception. Creating your own promises is just that easy and tends to make your code much smaller and easier to maintain. Here, we've packaged up at least five lines into a call that will take a single line and always intercept the error. Let's use the function we just created to get google.com and log the result. We can use the same function with async and await. Even though this acts like fetch, it doesn't return the same data structures. Instead, this returns the raw value as a string. We can parse it using the JSON class. Exception handling is easy using the catch keyword. Or if you use async and await, a try catch block. Most developers use the catch keyword or a try catch block to handle errors. But I also want to show you another approach that you might see. We have been using .then with a single function. This gets the value of the previous promise. But you can pass two functions to .then, one when the promise succeeds and one when it fails. So if you see two values, now you know the second is an error handler. So far, we're processing things one at a time, fetching single files. What if you wanted to do something in parallel? After all, the browser normally loads multiple files at the same time, so why shouldn't you? There are two methods that take an array of promises and run them in parallel, promise.all and promise.race. Let's take these one at a time. Promise.all takes an array of promises and resolves when all of them resolve or rejects if any of them reject. How will you know which one rejected? You have to go through the promises in the original list and see which one did. Note that even if an input promise rejects, causing promise.all to reject, the remaining promises still run and the results will be discarded. Promise.race runs multiple promises and resolves or rejects with the first promise to complete. Unfortunately, one promise might reject while the other one resolves, making this a slightly treacherous call. If you want to practice this, try the promises code lab. It will take you through all the techniques in this video. I hope you enjoy working with promises. They're a great tool for simplifying asynchronous code and making your apps more robust. Stay tuned for more on building progressive web apps and I'll see you soon. Thanks for watching.