Open In App
Related Articles

Why we use then() method in JavaScript ?

Improve Article
Improve
Save Article
Save
Like Article
Like

The then() method in JavaScript has been defined in the Promise API and is used to deal with asynchronous tasks such as an API call. Previously, callback functions were used instead of this function which made the code difficult to maintain.

Syntax:

demo().then(
    (onResolved) => {
        // Some task on success
    },
    (onRejected) => {
        // Some task on failure
    }
)

Note: demo is a function that returns a promise prototype.

Parameters: This function has two parameters to handle the success or rejection of the promise:

  • onFulfilled: This is a function that is called upon to the success of the promise. This is an optional parameter.
  • onRejected: This is a function that is called upon the rejection of the promise. This is an optional parameter.

Return Value: This method can either return a Promise (if further another then() is called) or nothing.

Example 1: Passing no arguments

JavaScript




function demo() {
    console.log("Function called!!<br>")
    return Promise.resolve("Success");
    // or
    // return Promise.reject("Failure");
}
demo().then()


 Output: 

Function called!!

Example 2: Passing only the first callback

JavaScript




function demo() {
    console.log("Function called!!")
    return Promise.resolve("Success");
    // or
    // return Promise.reject("Failure");
}
demo().then(
    (message) => {
        console.log("Then success:" + message);
    }
)


Output: 

Function called!!
Then success:Success

Note that if the demo function returns a reject then it will generate an error.

Example 3: Passing both the arguments

JavaScript




function demo() {
    console.log("Function called!!")
    return Promise.resolve("Success");
    // or
    // return Promise.reject("Failure");
}
demo().then(
    (message) => {
        console.log("Then success:" + message);
    },
    (message) => {
        console.log("Then failure:" + message);
    }
)


 
Output: 

Function called!!
Then success:Success

Example 4: Chaining Multiple then() methods.

Each then() can return a promise (a resolve or a reject) and therefore multiple then() methods can be chained together.

JavaScript




function demo() {
    console.log("Function called!!")
    return Promise.resolve(1);
    // or
    // return Promise.reject("Failure");
}
demo().then(
    (value) => {
        console.log(value);
    return ++value;
    },
    (message) => {
        console.log(message);
    }
    ).then(
    (value) => {
        console.log(value);
    },
    (message) => {
        console.log(message);
    }
)


 Output: 

Function called!!
12 

Example 5: Using then() as an asynchronous function 

JavaScript




var demo = new Promise((resolve, reject) => {
    resolve(1);
})
let call = demo.then(
    (value) => {
        console.log(value);
        return ++value;
    },
    (message) => {
        console.log(message);
    });
    console.log(call);
    setTimeout(() => {
    console.log(call);
});


Output: 

Promise {status: "pending"}
1
Promise {status: "resolved", result: 2}

Supported Browsers:

  • Google Chrome 6.0 and above
  • Internet Explorer 9.0 and above
  • Mozilla 4.0 and above
  • Opera 11.1 and above
  • Safari 5.0 and above

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 20 Nov, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials