Promises In JAVASCRIPT

Naseeb Shah
CodeX
Published in
2 min readJun 22, 2022

The promise is a JS special object. It produces value after an asynchronous operation is completed successfully or not. If it’s successful then perform a task which is defined resolve part. other hands, operation not successful code will be executed from the reject part.

Simply Promises is a JavaScript Object that links producing code and consuming code.

As you can see above example, line 1 we are producing a promise and this line has that function called the executor function. And after successful completion make a call then function and then is consumer function.

Understanding Promise States

Promises can be created by with the constructor syntax like this:___

The constructor function takes a function as an argument that is called the the executor function.

A promise has the following internal properties :
State: This property has three values .Pending , Fulfilled and Rejected.

  1. Pending-
    When the executor function start execution.
  2. Fulfilled- When promises is resolved.
  3. Rejected- When promises is rejected.

Result- This property has again three values.

  1. undefined — when the state is pending.
  2. value — when promises resolved (value) is called value.
  3. error — when promises rejected(error) is called error.

How to handle once promises you are created it

A promise uses an executor function to complete a task (mostly asynchronously).A consumer function should get notified when the executor function is done. The handler methods, .then(), .catch() and .finally(), help to create the link between the executor and the consumer functions so that they can be in sync when a promise resolves or rejects.

If the promise is fulfilled then the response goes to the .then() method for further actions .The promise is rejected the response goes to .catch() function.

Please give your valuable feedback and suggestions.
Thanks.

--

--