How to call promise inside another promise in JavaScript?

When working with JavaScript, you may find yourself needing to call a promise inside another promise. While this may seem like a daunting task, it is actually quite simple once you understand the basics of promises. In this article, we will discuss how to call a promise inside another promise in JavaScript.

The Basics of Promises

In order to understand how to call a promise inside another promise, it is important first to understand the basics of promises. A promise is an object that represents the eventual result of an asynchronous operation. Promises are used in JavaScript to handle asynchronous operations in a more synchronous way.

Basic Promise Chaining Approach

The most common way to call a promise inside another promise is through promise chaining using .then(). This allows you to execute promises sequentially.

<!DOCTYPE html>
<html>
<head>
    <title>Promise Inside Promise Example</title>
</head>
<body>
    <div id="result1"></div>
    <script>
        function func1(input) {
            return new Promise(function(resolve, reject) {
                if(input === "resolved") {
                    resolve("resolved");
                } else {
                    reject("rejected");
                }
            });
        }
        
        var promise = new Promise(function(resolve, reject) {
            func1("resolved").then(function(result) {
                resolve(result);
            }).catch(function(error) {
                reject(error);
            });
        });
        
        promise.then(function(result) {
            document.getElementById("result1").innerHTML = result; // "resolved"
        }).catch(function(error) {
            document.getElementById("result1").innerHTML = error; // "rejected"
        });
    </script>
</body>
</html>
resolved

Returning Promises from .then() Handlers

A cleaner approach is to return a new promise from within a .then() handler. This automatically chains the promises together.

<html>
<body>
    <h3>Calling a promise inside another promise</h3>
    <div id="output"></div>
    <script>
        let firstPromise = new Promise((resolve, reject) => {
            resolve("Hello, ");
        })
        .then((result) => {
            document.getElementById("output").innerHTML += result;
            return new Promise((resolve, reject) => {
                resolve(result + "<br> Welcome to JavaScript!");
            });
        })
        .then((result) => {
            document.getElementById("output").innerHTML += "<br>Welcome to JavaScript!";
        });
    </script>
</body>
</html>
Hello, 
Welcome to JavaScript!

Using async/await (Modern Approach)

The most readable way to handle promises inside promises is using async/await syntax, which makes asynchronous code look synchronous.

<html>
<body>
    <div id="result2"></div>
    <script>
        function fetchData(value) {
            return new Promise((resolve) => {
                setTimeout(() => resolve(`Data: ${value}`), 1000);
            });
        }
        
        async function processData() {
            try {
                const firstResult = await fetchData("Step 1");
                document.getElementById("result2").innerHTML += firstResult + "<br>";
                
                const secondResult = await fetchData("Step 2");
                document.getElementById("result2").innerHTML += secondResult + "<br>";
                
                return "All steps completed";
            } catch (error) {
                console.error("Error:", error);
            }
        }
        
        processData().then(result => {
            document.getElementById("result2").innerHTML += result;
        });
    </script>
</body>
</html>
Data: Step 1
Data: Step 2
All steps completed

Benefits and Best Practices

There are several benefits of properly handling promises inside other promises:

  • Sequential execution: Ensures operations happen in the correct order
  • Error handling: Centralized error management with .catch()
  • Readability: Makes asynchronous code more understandable

Best practices: Use async/await for better readability, avoid deeply nested promise chains (callback hell), and always include proper error handling.

Conclusion

Calling promises inside other promises is essential for sequential asynchronous operations. Use promise chaining with .then() or the modern async/await syntax for cleaner, more maintainable code.

Updated on: 2026-03-15T23:19:00+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements