Sign in

A promise is an object that is used to handle the computation of Asynchronous functions. The promise represents a value which may be available now, or in the future, or never.

For example, you sent an HTTP request to the server, and the server takes 5 seconds to process any request. so here expected 3 responses from sever.
1) Server send data back against the request
2) Server send an error message
3) Database Stuck

In this case, developers needed a way to express how the program should behave. Promises can handle the situation and provide three states in that case
1) Pending (Not fulfilled or Rejected)
2) Fulfilled (Operation Completed Successfully)
3) Rejected (Operation Failed)

If Promise Fulfilled, we use “.then” method.
If Promise Rejected, We use “.catch” method.

I found a simple example which may be easily graspable the actual concept.
var promise = new Promise( (resolve, reject) => {
let name = ‘Dave’
if (name === ‘Dave’) {
resolve(“Promise resolved successfully”);
}
else {
reject(Error(“Promise rejected”));
} });
promise.then(function(result) {
console.log(result); // “Promise resolved successfully”
}, err => {
console.log(err); // Error: “Promise rejected”
});

Fall in love with losing then you won't be scared.