Now that's a catchy title, but I promise you'll get it by the end of this post or not 😉 I can't promsie😊
What is a promise?
Why or When do we make a promise?
Suppose I made a promise to my math teacher that I'm gonna solve a math problem as my homework assignment:
let solveProblem = new Promise(function(resolve,reject))
//me trying to solve the problem
//usually code here contains, what we are supposed to do
//true if I solved it and false if I failed to solve the problem
//Teacher checking my status below
//What next if I solve it
//Teacher makes this move in-case I haven't solved the problem
The above code is very simple, first we are declaring a promise with the name solveProblem and
- resolve: represents fulfillment or meaning that the promise is sucessful i.e you did finish what you said you would.
- reject: is the action to be taken when the promise returns false meaning the promise is unfinished.
//if promise is fullfilled then do this
//if promise fails then do this
console.log('Try again, problem'+notSolved);
Here consider the above code as continuation, now after finding out what happened to the promise we have to do so action in response to the outcome hence we use the keyword
then to call a function, when the promise is sucessful else
catch to call a funtion when the promise fails.
And notice the arguments in both the funtions solvedProblem and notSolved these argumnets are passed from resolve and reject in their respective cases.
Summing up everything if the problem is solved and solve returns true next if executes resolve and then calls the function and we get the output:
Congratulations! Problem Solved
Try again, problem Not Solved.
But why use promises?
So now we got a basic understanding of promises, this can be also be done even with 'EventListener' functions,but why use them?
This can happen in asynchronous tasks and failure events, in that scenario we can use promises with then as a callback function to after the event is sucessful or a fail, so the appropriate callback is called based on the outcome of the event.
Advantages of Promises
- Multiple callbacks are possible by adding then as many times as we need them
- Promises can be chained, i.e one then of a promise can call the then of another promise which is very helpful when we want to check the happening of several events, like a chained reaction.
States of a promise
A promise is unsually in one of these theree states:
- pending: the initial state, the promise is neither sucessful nor a failure
- fulfilled: represents that the promise if sucessful
- rejected: represents that the promise in not fulfilled.
Promises are unlike final callbacks, they are fast and react on what needs to be done when an event is either sucessful or a un-fulfilled.
But a single promise can resolve or reject only once and that is final. Immediate resolve or reject can be called in the promise itself this prevents and avoid any kinds of errors before they even occur. And promises represent events that are already happening.