Member-only story
Mastering Asynchronous Programming With JavaScript Promises
Learn how you can utilize JavaScript promises in your applications to handle asynchronous code

Dealing with asynchronous code can be a hard concept to understand, especially for newer developers. Fortunately for us, JavaScript promises make dealing with these types of situations much easier.
“Controlling complexity is the essence of computer programming.” — Brian Kernigan
Like Brian said, being able to control complicated situations as a developer is what programming is all about. To become great developers, we must familiarize ourselves with complex concepts such as this.
In this article, we will look at what promises are, and why we should use them. We will also look into why chaining promises is important, and even see how the method Promise.all()
can help us consolidate multiple promises into one.
Let’s get to it!
What is a promise?
A promise is a JavaScript object whose job is to handle asynchronous code. They allow us to postpone blocks of code until the action is either fulfilled or rejected. Instead of passing callbacks to a function, they’re attached to the end of a promise, giving us better readability and the opportunity to utilize chaining.
These are not to be confused with async/await
.
To truly understand how promises work, we should start by looking at an example using callbacks, and then go from there.
Here we have an example of a movie ticket kiosk that utilizes callbacks to handle our asynchronous code. Both selectMovie()
and getMovieTicket()
, pass two callbacks as arguments. The first one is called if the transaction is successful, and the other if it fails.
Since both selectMovie()
and getMovieTicket()
are asynchronous functions, we use callbacks to postpone the…