JSFiddle - React, Tailwind, and code Playground

by Sandeep Agrawal

HTML

<pre id="output"></pre>

JavaScript

//First part: Create the Promise and define the success/not success conditions.
let promise1 = new Promise( (resolve, reject) => {
    let isCallResolve = true; 

    if (isCallResolve) {
        resolve('Promise is successful and resolve method is called that is code inside then() !');
    }

    if (!isCallResolve) {
        reject('Promise failed and reject method is called that is code inside catch() !');
    }
});


//Second part: Define the actions for when the conditions are fulfilled.
promise1.then( (message) => {
    // then() method is the definition for resolve() callback method.
    // Execute code inside here if the promise is resolved i.e. is successful.
    document.getElementById('output').innerHTML += message;
}).catch( (message) => {
    // catch() method is the definition for reject() callback method.
    // Execute code inside here if the promise is is rejected that is not successful.
    document.getElementById('output').innerHTML += message;
});