This article aims to provide the introduction of Promises in JavaScript.
A promise is an object which can be returned synchronously from an asynchronous function.
It will be in one of the 3 possible states:
Fulfilled: onFulfilled() will be called (e.g., resolve() was called)
Rejected: onRejected() will be called (e.g., reject() was called)
Pending: not yet fulfilled or rejected
States of Promises are shown below :
A promise is settled if it’s not pending. Sometimes people use resolved and settled to mean the same thing, that it is not pending.
Once settled, a promise can not be resettled. Calling resolve() or reject() again will have no effect.
Example:
- Rules
- A promise or “thenable” is an object that supplies a standard-compliant .then() method.
- A pending promise may transition into a fulfilled or rejected state.
- A fulfilled or rejected promise is settled, and must not transition into any other state.
- Once a promise is settled, it must have a value (which may be undefined). That value must not change.
Change in this context refers to identity (===) comparison. An object may be used as the fulfilled value, and object properties may mutate.
Every promise must supply a .then() method with the following signature:
promise.then(
onFulfilled?: Function,
onRejected?: Function
) => Promise - Both onFulfilled() and onRejected() are optional.
- If the arguments supplied are not functions, they must be ignored.
- onFulfilled() will be called after the promise is fulfilled, with the promise’s value as the first argument.
- onRejected() will be called after the promise is rejected, with the reason for rejection as the first argument. The reason may be any valid JavaScript value, but because rejections are essentially synonymous with exceptions, I recommend using Error objects.
- Neither onFulfilled() nor onRejected() may be called more than once.
- .then() may be called many times on the same promise. In other words, a promise can be used to aggregate callbacks.
- .then() must return a new promise, promise2.
- If onFulfilled() or onRejected() return a value x, and x is a promise, promise2 will lock in with (assume the same state and value as) x. Otherwise, promise2 will be fulfilled with the value of x.
If either onFulfilled or onRejected throws an exception e, promise2 must be rejected with e as the reason.
If onFulfilled is not a function and promise1 is fulfilled, promise2 must be fulfilled with the same value as promise1.
If onRejected is not a function and promise1 is rejected, promise2 must be rejected for the same reason as promise1.
Promise Chaining
Because .then() always returns a new promise, it’s possible to chain promises with precise control over how and where errors are handled. Promises allow you to mimic normal synchronous code’s try/catch behavior.
Below find the Example For Promise Chaining :
Error Handling
Note that promises have both a success and an error handler, and it’s very common to see code that does this:
save().then(
handleSuccess,
handleError
);
But what happens if handleSuccess() throws an error? The promise returned from .then() will be rejected, but there’s nothing there to catch the rejection — meaning that an error in your app gets swallowed. Oops!
For that reason, some people consider the code above to be an anti-pattern, and recommend the following, instead:
save().then(handleSuccess).catch(handleError) ;
The difference is subtle, but important. In the first example, an error originating in the save() operation will be caught, but an error originating in the handleSuccess() function will be swallowed.
I抳e read several good stuff here. Definitely worth bookmarking for revisiting. I wonder how much effort you put to create such a fantastic informative web site.
Thanks for the publish. I have constantly observed that almost all people are desperate to lose weight since they wish to look slim and attractive. However, they do not often realize that there are more benefits to losing weight as well. Doctors claim that over weight people are afflicted with a variety of illnesses that can be instantly attributed to their own excess weight. The great news is that people that are overweight plus suffering from a variety of diseases are able to reduce the severity of their own illnesses by means of losing weight. It is easy to see a progressive but marked improvement with health when even a minor amount of fat reduction is obtained.
I know this if off topic but I’m looking into starting my own weblog and was wondering what all is needed to get setup? I’m assuming having a blog like yours would cost a pretty penny? I’m not very internet savvy so I’m not 100 sure. Any tips or advice would be greatly appreciated. Thanks