DEV Community

Cover image for Promise in Js
Vigneshwaran V
Vigneshwaran V

Posted on

Promise in Js

A JavaScript Promise is an object representing the eventual completion or failure of an asynchronous operation. It acts as a temporary placeholder for a value that is not available yet, allowing you to write asynchronous code that resembles synchronous logic.

Think of it like ordering food online:

  • Pending → Order placed, waiting for preparation.

  • Fulfilled → Food delivered successfully.

  • Rejected → Order failed or canceled.

JavaScript uses Promises to handle tasks that take time, such as:

  • Fetching data from an API

  • Reading files

  • Database operations

  • Timers (setTimeout)

A Promise is a placeholder for a value that will be available now, later, or never.

Why Do We Use Promises?

JavaScript executes code line by line (single-threaded).
Some operations take time:

  • Fetching student details from a server

  • Loading marks

  • Loading attendance

  • Loading fee information

Without Promises, we would use nested callbacks, leading to Callback Hell.


Understanding with One Real Example

Imagine a Student Portal.
We need:

  • Student Profile

  • Student Marks

  • Student Attendance
    Each request takes time.

Callback Hell

function getProfile(callback) {

    setTimeout(() => {
        console.log("Profile Loaded");
        callback();
    }, 1000);

}

function getMarks(callback) {

    setTimeout(() => {
        console.log("Marks Loaded");
        callback();
    }, 1000);

}

function getAttendance(callback) {

    setTimeout(() => {
        console.log("Attendance Loaded");
        callback();
    }, 1000);

}

getProfile(() => {

    getMarks(() => {

        getAttendance(() => {

            console.log("Student Dashboard Ready");

        });

    });

});
Enter fullscreen mode Exit fullscreen mode

Ouput

Profile Loaded
Marks Loaded
Attendance Loaded
Student Dashboard Ready
Enter fullscreen mode Exit fullscreen mode

Problem:

getProfile
   ↓
getMarks
   ↓
getAttendance
   ↓
Dashboard
Enter fullscreen mode Exit fullscreen mode

As tasks increase, nesting becomes messy.


Promise Version

Convert each operation into a Promise.

function getProfile() {

    return new Promise(resolve => {

        setTimeout(() => {
            resolve("Profile Loaded");
        }, 1000);

    });

}

function getMarks() {

    return new Promise(resolve => {

        setTimeout(() => {
            resolve("Marks Loaded");
        }, 1000);

    });

}

function getAttendance() {

    return new Promise(resolve => {

        setTimeout(() => {
            resolve("Attendance Loaded");
        }, 1000);

    });

}

//Promise Chaining

//Tasks depend on each other.

getProfile()
.then(result =>{
    console.log(result);
    return getMarks();
})
.then(result=>{
    console.log(result);
    return getAttendance();
})
.then(result=>{
    console.log(result);
    console.log("Student Dashboard Ready");
})
Enter fullscreen mode Exit fullscreen mode

Output

Profile Loaded
Marks Loaded
Attendance Loaded
Student Dashboard Ready
Enter fullscreen mode Exit fullscreen mode

Much cleaner than Callback Hell.


see the below example code and how its working in all the static functions of Promise.

function getProfile() {

    return new Promise(resolve => {

        setTimeout(() => {
            resolve("Profile Loaded");
        }, 1000);

    });

}

function getMarks() {

    return new Promise(resolve => {

        setTimeout(() => {
            resolve("Marks Loaded");
        }, 1000);

    });

}

function getAttendance() {

    return new Promise(resolve => {

        setTimeout(() => {
            resolve("Attendance Loaded");
        }, 1000);

    });

}
Enter fullscreen mode Exit fullscreen mode

Promise.all()

Use when ALL tasks must succeed.
Example:
Profile, Marks, and Attendance can load simultaneously.

Promise.all([
    getProfile(),
    getMarks(),
    getAttendance()
])
.then(results => {

    console.log(results);

});
Enter fullscreen mode Exit fullscreen mode

Output:

[ "Profile Loaded", "Marks Loaded", "Attendance Loaded" ]
Enter fullscreen mode Exit fullscreen mode

Rule

All Success → Success
One Failure → Entire Promise Fails
Enter fullscreen mode Exit fullscreen mode

Example:

Promise.all([
    getProfile(),
    Promise.reject("Marks Server Down"),
    getAttendance()
])
.then(result=>{
    console.log(result);
})
.catch(error => {

    console.log(error);

});
Enter fullscreen mode Exit fullscreen mode

Output

Marks Server Down
Enter fullscreen mode Exit fullscreen mode

Promise.allSettled()

Use when you want every result, even failed ones.

Promise.allSettled([
    getProfile(),
    Promise.reject("Marks Server Down"),
    getAttendance()
])
.then(results => {

    console.log(results);

});
Enter fullscreen mode Exit fullscreen mode

Output

[
  { status: "fulfilled", value: "Profile Loaded"},
  { status: "rejected", reason: "Marks Server Down"},
  { status: "fulfilled", value: "Attendance Loaded"}
]
Enter fullscreen mode Exit fullscreen mode

Rule

Success + Failure
      ↓
Return Everything
Enter fullscreen mode Exit fullscreen mode

Useful for dashboards where some widgets can fail.

Promise.race()

Use when you only care about the FIRST response.
Suppose data comes from multiple servers.

const server1 =
    new Promise(resolve =>
        setTimeout(() => resolve("Server 1"), 3000)
    );

const server2 =
    new Promise(resolve =>
        setTimeout(() => resolve("Server 2"), 1000)
    );

const server3 =
    new Promise(resolve =>
        setTimeout(() => resolve("Server 3"), 2000)
    );

Promise.race([
    server1,
    server2,
    server3
])
.then(result => {

    console.log(result);

});
Enter fullscreen mode Exit fullscreen mode

Output

Server 2
Enter fullscreen mode Exit fullscreen mode

Rule

First Settled Wins
Enter fullscreen mode Exit fullscreen mode

Success or failure doesn't matter.

Promise.any()

Use when you need the FIRST SUCCESS.

const server1 =
    Promise.reject("Server 1 Failed");

const server2 =
    new Promise(resolve =>
        setTimeout(() => resolve("Server 2 Success"), 2000)
    );

const server3 =
    new Promise(resolve =>
        setTimeout(() => resolve("Server 3 Success"), 3000)
    );

Promise.any([
    server1,
    server2,
    server3
])
.then(result => {

    console.log(result);

});
Enter fullscreen mode Exit fullscreen mode

Output

Server 2 Success
Enter fullscreen mode Exit fullscreen mode

Rule

Ignore Failures
Return First Success
Enter fullscreen mode Exit fullscreen mode

Only fails if ALL promises fail.


These are static methods of the Promise object that help you work with multiple promises at the same time.


References

https://www.geeksforgeeks.org/javascript/javascript-promise/
https://www.w3schools.com/js/js_promise.asp
https://www.programiz.com/javascript/promise

Top comments (4)

Collapse
 
raja_b_0c9d242e2c26cf063b profile image
Raja B

Mr.Vignesh

why this reson for use promise???

promise basic structure???

Collapse
 
vigneshwaran_v profile image
Vigneshwaran V

Hi bro,

  1. We use Promises to handle asynchronous operations such as API calls, database queries, file reading, and timers in a clean and readable way.

Before Promises, developers often used nested callbacks, which could lead to "Callback Hell" and make code difficult to read and maintain.

Promises provide:
• Better readability
• Better error handling using .catch()
• Easier chaining of asynchronous operations using .then()
• More maintainable code

In simple terms, a Promise represents a value that will be available in the future and lets us handle the result when it arrives.

  1. The basic structure of a Promise is:
const promise = new Promise((resolve, reject) => {

    // Asynchronous operation

    if (success) {
        resolve("Success");
    } else {
        reject("Failed");
    }

});

promise
    .then(result => {
        console.log(result);
    })
    .catch(error => {
        console.log(error);
    })
    .finally(() => {
        console.log("Completed");
    });
Enter fullscreen mode Exit fullscreen mode

Here:

new Promise() creates a Promise object.
resolve() is called when the operation succeeds.
reject() is called when the operation fails.
.then() handles the success result.
.catch() handles errors.
.finally() runs regardless of success or failure.

Collapse
 
vigneshwaran_v profile image
Vigneshwaran V

bro I write the Example code, just see how it works in Callback Hell and also in Promise, then you get some clarity.

Collapse
 
raja_b_0c9d242e2c26cf063b profile image
Raja B

understand Mr.vignesh Thank you!