DEV Community

Cover image for Tidbit 04: JavaScript Promise Chain vs No-Chain
Tapas Adhikary
Tapas Adhikary

Posted on • Edited on • Originally published at tapascript.io

1

Tidbit 04: JavaScript Promise Chain vs No-Chain

Promise Chain vs. No-Chain

A promise chain is formed when we append multiple promise handler functions(like, .then() or .catch()) to handle results and errors. However, at times you could be mistaken that you need to prefix the promise everytime you want to form a chain.

Let's understand it with the following examples:

First, here is a simple promise created with the Promise() constructor function. The promise resolves immidiately with a result value of 10.

const ten = new Promise((resolve, reject) => {
    resolve(10);
});
Enter fullscreen mode Exit fullscreen mode

Now, to form a Promise Chain, let us chain a bunch of .then() handler functions:

// Promise Chain

ten.then((result) => {
    // returns 20
    return result + 10;
})
.then((result) => {
    // returns 200
    return result * 10;
})
.then((result) => {
    // returns 190
    return result - 10;
})
.then((result) => {
    // logs 190 in console
    console.log(result);
});
Enter fullscreen mode Exit fullscreen mode

It has formed a perfect Promise Chain to pass the result from one handler function to another, and finaly print the end result into the console. The final result output is a number, i.e, 190.

Now, let us change the above code to the following:

// No Chain

ten.then((result) => {
    // returns 20
    return result + 10;
});
ten.then((result) => {
    // returns 100
    return result * 10;
});
ten.then((result) => {
    // returns 0
    return result - 10;
});
ten.then((result) => {
    // logs 10 in the console.
    console.log(result);
});
Enter fullscreen mode Exit fullscreen mode

As you notice, we have now prefixed the promise ten every time before the .then() handler function. It is not a chain because, we are handling the promise freshly every time with

ten.then((result) => {
    // DO SOMETHING...
});
Enter fullscreen mode Exit fullscreen mode

We are not passing the resolved result of the previous promise to the next one. Hence the output will be, 10. It is a common mistake developers make with JavaScript Promises.

Chain vs. No Chain

Want to learn further?

Learn about other common mistakes developers make in handling JavaScript Promises.

Day 26: 6 Common Mistakes with JavaScript Promises & Async Code 🤩 - In this session, we’ll break down 6 common mistakes developers make while working with Promises and async code — and most importantly, how to avoid them! Whether you’re just learning or brushing up, this video will help you write cleaner, bug-free asynchronous code.

Warp.dev image

The best coding agent. Backed by benchmarks.

Warp outperforms every other coding agent on the market, and gives you full control over which model you use. Get started now for free, or upgrade and unlock 2.5x AI credits on Warp's paid plans.

Download Warp

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments. Some comments have been hidden by the post's author - find out more

Gen AI apps are built with MongoDB Atlas

Gen AI apps are built with MongoDB Atlas

MongoDB Atlas is the developer-friendly database for building, scaling, and running gen AI & LLM apps—no separate vector DB needed. Enjoy native vector search, 115+ regions, and flexible document modeling. Build AI faster, all in one place.

Start Free

GV x Bolt: Building Bold Ideas

Join us for a conversation between general partner at GV, Erik Nordlander and Bolt Co-Founder, Eric Simons. They’ll explore the early days of StackBlitz, the search for product-market fit, and what it takes to build a business in today’s landscape.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️