Sign in
Log inSign up
Understanding Promises🤝 in JavaScript

Understanding Promises🤝 in JavaScript

Tejas Shah's photo
Tejas Shah
·Oct 15, 2021·

3 min read

Promises are the result of an asynchronous operation in JavaScript. The result is a value describing state & value returned by the operation. In simple terms, a promise is the result of an action that is going to be completed in future.

Syntax

A Promise can be created with the constructor syntax, that takes a function to execute. It is called Executor Function. The executor function takes two callbacks as arguments - resolve and reject.

let promise = new Promise(function (resolve, reject){
    // code to execute
})

The 3 States of a Promise

Generally, a promise object can be in any of the following three states. The 3 results/states of a Promise Object are:

StateValue ReturnedMeaning
Pending (Unsettled State)UndefinedThe operation is running
Fulfilled (Settled State)ValueThe operation is settled with some value returned
Rejected (Settled State)ReasonThe operation is settled with an error

PENDING Promise

Pending.png

In the above image, I have declared a promise with an empty Executor Function. Hence the promise will be in Pending State.

FULFILLED Promise

fulfilled.png

In the above image, I have declared a Promise that gets resolved upon executing the Executor Function. Hence, It returns a value.

REJECTED Promise

rejected.png

In the above image, I have declared a Promise that gets rejected upon executing the Executor Function. Hence, It returns a reason regarding why the promise was rejected.

Handling Promises

  • Let me tell you that you cannot access the value returned by a promise object by using the (.)dot operator. JavaScript provides methods for accessing them.

  • In general, we use .then & .catch methods to perform any action when a promise is settled.

then Method

The .then() method is executed when a promise is resolved. When it is attached to a promise, the value returned by the Promise object, automatically gets passed to the .then() method.

catch method

The .catch() method gets executed when a promise is rejected. When a .catch() method is attached to a promise, the reason(error message) returned by the Promise Object, automatically gets passed to the .catch() method.

finally method

We also have .finally() method which gets executed irrespective of the Promise getting resolved or rejected.

Example

Let us consider, You want to understand Promises by my blog. Here there are two chances regarding you have understood or not.

understood = yes (Noice!🤩 Like & Practice more)

understood = no (Oh!😢 This was me. Check References)

So if this example is considered in terms of JavaScript then it would be the following:

ExampleJavaScript
YouPromise
You reading the postAsync Operation in JS
Post Understood/NotPromise Return Value
Post UnderstoodPromise Fulfilled
Post not understoodPromise Rejected
Like & Practice MoreSuccess Callback(Resolve)
Checking ReferencesFailure Callback(Reject)

Let's code them :-

Promise Example.png

Execute Promise.png

If the value of understood is true then the resolve callback is executed. Similarly, if the value is false then reject callback is executed.

Why to use Promises

Promise(s) are the antidote to Callback-hell. Instead of multiple callbacks or nested callbacks, Promises serve the purpose.

References