VOOZH about

URL: https://thenewstack.io/what-are-promises-in-javascript/

⇱ Promises in JavaScript: A Primer - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2022-11-25 03:00:03
Promises in JavaScript: A Primer
tutorial,
Software Development

Promises in JavaScript: A Primer

Promises provide a way to write asynchronous operations in JavaScript, a single-threaded language.
Nov 25th, 2022 3:00am by Jessica Wachtel
👁 Featued image for: Promises in JavaScript: A Primer

The following article covers a brief introduction to the concept of Promises. Promises are new-ish to JavaScript (introduced in ES6), and often misunderstood. The concept is basically a repurposing of familiar ideas in fancy branding.

Promises are a way to execute asynchronous code. Because JavaScript is a single threaded language, tools have to be created for this to happen since it doesn’t happen natively.

I’ll go over two detailed examples of what a promise can look like, how .then statements are chained together, and what causes a .catch to trigger and where it can trigger. The last topic will be the order of execution when promises are executed in multiple lines of code and what a successful promise resolution versus what a failed promise looks like from an external API.

How Asynchronous Code Is Different

Synchronous code is easy to understand. It’s much like writing itself.

I’m writing this line first. And now it’s read first.

Whoop, we’re here now. This line is executing now.

And so on.

And so forth.

But with asynchronous code, the code files are more like recipes. Read a little, measure, chop, and add more garlic… There are instructions, actions, and through text, the lines may start off as read in order, but different processes take longer than others. All that said, at no time does the application or dinner process stop because a server response or shrimp skewer takes longer than expected to complete.

The Anatomy of a Promise

A promise is an object that represents the eventual fulfillment of an asynchronous operation. It exists in three states:

  • Pending: this is the starting place. The “promise” that at some point there will be an answer to this question even if the application continues to execute code further down in the file.
  • Fulfilled/resolved: The promise was successfully resolved. The condition met was true or data was returned.
  • Failed/rejected: The result of the promise if an error took place during the fulfillment of the promise.

Also, know these methods:

  • .then: The JavaScript method used to handle a successful promise.
  • .catch: How the errors are caught.

👁 Image

A new promise class is created with two parameters, resolve and reject. In this very basic example, there is a value. If the string value is correct it will resolve “correct” and if it’s incorrect then it will reject “error”. Simple.

Let’s resolve the promise.

👁 Image

👁 Image

And here’s what it looks like if the conditional statement is false.

👁 Image

👁 Image

That is incredibly basic and not a likely use case for promises.

Avoiding Callback Hell

Callback hell: An ambiguous yet descriptive term. Too many callbacks. Deep nesting. The functionality usually isn’t compromised but readability is a nightmare and that tends to compromise functionality especially when it comes to code updates especially when additional engineers are working on it.

👁 Image

The image above is the beginning of callback hell (from my article 3 Types of Asynchronous Programming). If you google callback hell there are many many worse images that can’t be included in this article for copy write reasons.

👁 Image

The example above builds off of the first example. This function takes in a data structure and if it is in fact an array, it resolves the sum of its elements, if not then rejects the promise. The answer is console.log 29.

What if a string is passed in?

👁 Image

Passing in {e}will return the error as an error object which is useful in more advanced use cases.

👁 Image

Perfect, it works.

What if we want to take the sum, find the remainder after it’s divided by two then subtract 50? .thens can be chained and it will look like this. Keep passing data from one to the next and along we go.

👁 Image

And the console logs look like this:

👁 Image

But if there happens to be an error, such as an undefined variable being subtracted by one of those values, the error object will display.

👁 Image

👁 Image

Great example, but like the first one, also not async code.

Async Example — HTTP Requests

For this example, I’m using the Star Wars API. Here’s the code:

👁 Image

The console logs look like this:

👁 Image

But if the code changes ever so slightly to delayed 1/4 second rather than delayed 1 second:

👁 Image

The console.logs will look like this:

👁 Image

The console.logs look like this:

👁 Image

And here is an example of promise chaining when receiving data in the API request.

👁 Image

If a request was made to a bad API for example: 👁 Image

Then you could either console.log a specific message or the entire error object. The error object in this case looks like this:

👁 Image

Additional Learning

This was an incredibly brief and basic introduction to promises. The learning gets pretty heavy after this initial stop. For a deeper dive into the material, this  a good place to look. Codingame is a site where you can start working with async code and promises for a more hands on learning experience.

TRENDING STORIES
Jessica Wachtel is a developer marketing writer at InfluxData where she creates content that helps make the world of time series data more understandable and accessible. Jessica has a background in software development and technical journalism.
Read more from Jessica Wachtel
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.