Promises are a method by which we can handle Asynchronous Requests in a better way. Normally a Promise has three output conditions:
a. RESOLVED
b. PENDING
c. REJECT
Just imaging a Promise that your father made, for buying a good phone. It would have three conditions:
a. RESOLVED - Your father got you a new phone.
b. REJECTED - You didn't got a new phone.
c. PENDING - The promise is neither rejected or fulfilled, still you are waiting for the phone
a. RESOLVED
b. PENDING
c. REJECT
Just imaging a Promise that your father made, for buying a good phone. It would have three conditions:
a. RESOLVED - Your father got you a new phone.
b. REJECTED - You didn't got a new phone.
c. PENDING - The promise is neither rejected or fulfilled, still you are waiting for the phone
function checkValue(x){
if(x>100){
return Promise.resolve(x);
}
else{
return Promise.reject("does not satisfy the condition");
}
}
var result = checkValue(20);
console.log("this is executing");
console.log(result); //This returns an Object of Promise which we can use.
var t = function(){
//The object of Promise can be used to use Then or use Catch for the Error.
result.then(function (fulfilled) {
// the promise worked
console.log("Promise worked as per my condition");
console.log(fulfilled);
})
.catch(function (error) {
console.log(error);
});
};
t();
In the above code, we can see that function returns an object Promise, based on the
value of X and then you can capture that Promise to further perform any action.
Happy Coding!.
Comments