Javascript Promise is a special JavaScript object that links the “producing code” and the “consuming code” together. The promise can only succeed or fail once. It cannot succeed or fail twice or more, neither can it switch from the success to failure or vice versa. If the promise has succeeded or failed and you later add the success/failure callback, the correct callback will be called based on the output, even though the event took place earlier.
Till now, we learned about events and callback functions for handling the data. But, its scope is limited. It is because events were not able to manage and operate asynchronous operations. Thus, Promise is the simplest and better approach for handling asynchronous operations efficiently.
There are two possible differences between Promise and Event Handlers:
A promise can be present in one of the following states:
Thus, a promise represents the completion of an asynchronous operation with its result. It can be either successful completion of the promise, or its failure, but eventually completed. Promise uses a then() which is executed only after the completion of the promise resolve.
A JavaScript Promise promises that:
The functions of Promise are executable almost on every trending web browsers such as Chrome, Mozilla, Opera, etc. The methods list is:
Method Name | Summary |
---|---|
Promise.resolve(promise) | This method returns promise only if promise.constructor==Promise. |
Promise.resolve(thenable) | Makes a new promise from thenable containing then(). |
Promise.resolve(obj) | Makes a promise resolved for an object. |
Promise.reject(obj) | Makes a promise rejection for the object. |
Promise.all(array) | Makes a promise resolved when each item in an array is fulfilled or rejects when items in the array are not fulfilled. |
Promise.race(array) | If any item in the array is fulfilled as soon, it resolves the promise, or if any item is rejected as soon, it rejects the promise. |
new Promise(function(resolve, reject){}); | Here, resolve(thenable) denotes that the promise will be resolved with then(). Resolve(obj) denotes promise will be fulfilled with the object Reject(obj) denotes promise rejected with the object. |
<html>
<head>
<h2> Javascript Promise</h2>
</br> </head>
<body>
<script>
var p=new Promise(function(resolve, reject){
var x= 2+3;
if(x==5)
resolve(" executed and resolved successfully");
else
reject("rejected");
});
p.then(function(fromResolve){
document.write("Promise is"+fromResolve);
}).catch(function(fromReject){
document.write("Promise is "+fromReject);
});
</script>
</body>
</html>
In the above Promise implementation, the Promise constructor takes an argument that callbacks the function. This callback function takes two arguments, i.e.,
It means either resolve is called or reject is called. Here, then() has taken one argument which will execute, if the promise is resolved. Otherwise, catch() will be called with the rejection of the promise.
Thanks for reading!!!
#javascript #producing code #consuming code