Write JavaScript Promise with examples

In this tutorial, we will learn how to write a function in JavaScript Promise with examples. In this, we use mainly resolve or reject.

JavaScript Promise
  • A JavaScript Promise is used for reject or resolve the statement.
  • JavaScript Promise use the then(), catch() methods mainly.
  • For this we can take the constant values also

JavaScript Promise States:

  • Pending
  • Resolve
  • Rejected

Promises have the Main Methods :

  • ‘then()’
  • ‘catch()’

JavaScript Promise Example

<html>
<head>
<title>Promise Example</title>
<script>
let promise = new Promise(function (resolve, reject) {
    const x = "English";
    const y = "Maths"
    if (x === y) {
        resolve();
    } else {
        reject();
    }
});

promise.
    then(function () {
        console.log('Success, You are a Promise');
    }).
    catch(function () {
        console.log('Some error has occurred');
    });     
</script>
</head>
<body>
</body>
</html>

Output:

Some error has occurred

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top