Better Programming

Advice for programmers.

Follow publication

Member-only story

Mastering Asynchronous Programming With JavaScript Promises

Michael Miller
Better Programming
Published in
6 min readSep 29, 2021

Photo by John Schnobrich on Unsplash

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…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Michael Miller
Michael Miller

Written by Michael Miller

Full-Stack Developer, React.js, Javascript, MySQL, Node.js. Subscribe to The Pro Code for weekly coding updates: https://bit.ly/3zDSGyW

No responses yet