Skip to content
Related Articles

Related Articles

Improve Article

Why we use then() method in JavaScript ?

  • Difficulty Level : Medium
  • Last Updated : 30 Jun, 2020

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 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




<script type="text/javascript">
    function demo() {
      document.write("Function called!!<br>")
      return Promise.resolve("Success");
      // or
      // return Promise.reject("Failure");
    }
    demo().then()
 </script>

Output: 

Function called!!

Example 2: Passing only the first callback

JavaScript




<script type="text/javascript">
    function demo() {
      document.write("Function called!!<br>")
      return Promise.resolve("Success");
      // or
      // return Promise.reject("Failure");
    }
    demo().then(
      (message) => {
        document.write("Then success:" + message);
      }
    )
</script>

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




    
<script type="text/javascript">
    function demo() {
      document.write("Function called!!<br>")
      return Promise.resolve("Success");
      // or
      // return Promise.reject("Failure");
  
    }
    demo().then(
      (message) => {
        document.write("Then success:" + message);
      },
      (message) => {
        document.write("Then failure:" + message);
      }
    )
</script>

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




    
<script type="text/javascript">
    function demo() {
      document.write("Function called!!<br>")
      return Promise.resolve(1);
      // or
      // return Promise.reject("Failure");
  
    }
    demo().then(
      (value) => {
        document.write(value);
        return ++value;
      },
      (message) => {
        document.write(message);
      }
    ).then(
      (value) => {
        document.write(value);
      },
      (message) => {
        document.write(message);
      }
    )
</script>

Output: 

Function called!!
12

Example 5: Using then() as an asynchronous function

JavaScript




<script type="text/javascript">
    var demo = new Promise((resolve, reject) => {
      resolve(1);
    })
    let call = demo.then(
      (value) => {
        console.log(value);
        return ++value;
      },
      (message) => {
        document.write(message);
      });
    console.log(call);
    setTimeout(() => {
      console.log(call);
    });
</script>

Console Output: 

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

Attention reader! Don’t stop learning now. Get hold of all the important Comcompetitivepetitve Programming concepts with the Web Design for Beginners | HTML  course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :