Learn about the new pattern to handle asynchronous code, called promises, and the new API that they enable.
- [Instructor] The callback pattern is a fundamental part of writing code in node.js and Java script. While it works easily enough for most simple cases, as complexity grows callbacks can cause code to become extremely convoluted and difficult to understand. For some time there has been a proposed fix to the callback pattern and this structure is called a promise. Many libraries implemented the idea of a promise and made it available in ES5 like Q and Bluebird. But with ES6 we finally have a native promise implementation in node.js.
Here's an example of a typical callback operation where a function is passed into another function as the argument and that argument function is called once some actions have been completed. Additionally, this shows the typical node callback pattern where the first argument passed to the callback is an error object if any and the resulting data as the second argument. In this example the bakeAsync function does not return a value when called instead passing the result of its execution to a function.
The essence of the callback pattern. Promises flip this callback concept on its head and return to the idea that a function should return something. Instead of passing in a callback to an asynchronous function, you get back a promise as the returned object. The ES6 promise object itself is a constructor function that is used to create a new promise object. You use this constructor to create a promise object to return from a function. The return promise object has a very simple API with only two functions, each with a very specific purpose.
The first API function is then and it defines what you want to happen once the promise has successfully been executed. The argument to then is a function that is called with the result of whatever you expect from the promise. The other API function on promise objects is catch and the function argument passed then to it will be called if an error scenario happens. Here is the previous callback example rewritten with promises. The bakeAsync function would return a newly constructed promise object.
When bakeAsync is called, will chain function calls for then and catch passing in the functions we want to be called. Now while this looks like more code than the previous example, it is much more expressive and explicit with what is happening. There's a clear asynchronous contract established between the bakeAsync function and any callers. All brought about by the promise pattern. Now we are returning a promise object but where do we put our Async code? And how are then and catch called? The argument to a promise constructor is a function that accepts two arguments, typically called resolve and reject.
These are both functions that you can call inside this function body that will trigger the then functionin the case of resolve or the catch function in the case of reject. The body of that function would be similar to any other callback type function. You do some stuff usually asynchronously then call resolve or reject when you would have called a callback. So here's a final complete example of an asynchronous function returning a promise and that function being called and the promise API utilized.
Your asynchronous logic would be done here. Then the result of it evaluated and the appropriate response type function called. From the calling side, both positive and negative outcomes have handlers that will be executed based on the result. All of the syntax you're used to is still here but promises represent a new way to conceptualize and structure asynchronous code.
- Using arrow functions
- Using default parameters
- Reviewing the class structure in ES6
- Assigning variables with let and const
- Array destructuring and object destructuring
- Replacing callbacks with promises
- Keeping values unique with Set
- Dropping the prototype with Map
- Working with generators
- Using yield with arguments