Promises
Explore how to use Promises to manage asynchronous code in JavaScript, replacing nested callbacks with a more readable syntax. Learn to chain .then methods and convert callback-based functions into Promises for cleaner asynchronous programming.
We'll cover the following...
Preface: A Note
Throughout this lesson and future lessons regarding asynchronous code, several code blocks will be runnable. The code in the blocks is not runnable by itself; if you try to paste it in another JavaScript evaluator, it will not work.
There is hidden code prepended in each block. These blocks are meant only to demonstrate what an actual application using asynchronous code might look like.
Introduction
We come now to a solution to the pyramid of doom from earlier. Promises are still entirely callback-based, but they give us a nice syntax wrapper around the callbacks to make it easier to read asynchronous code.
Standard Callbacks
Starting with callbacks, let’s review a common pattern of asynchronous code execution. This is the same code found in the earlier lesson. Here’s the situation we’re in.
A user ...