Kumar Jitendra
5 min readNov 14, 2023

JavaScript — Promises Introduction to handle the Asynchronous calls in Java Script.

Promises Definition in Java Script: -

Previous blog Description — JavaScript — Powerful language to handle the Synchronous and Asynchronous call. | by Kumar Jitendra | Nov, 2023 | Medium

In my previous blog — we discussed around the Synchronous and Asynchronous operations in the JavaScript. Promises in JavaScript is an effective way of handling the Asynchronous call in the Java Script.

It is used to find out if the asynchronous operation is successfully completed or not.

A promise may have one of three states.

  • Pending
  • Fulfilled
  • Rejected

A promise starts in a pending state. That means the process is not complete. If the operation is successful, the process ends in a fulfilled state(resolved). And, if an error occurs, the process ends in a rejected state.

Steps to Create a Promise in Java Script:

To create a promise object, we use the Promise () constructor.

let promise = new Promise (function (resolve, reject) {
//do something
});

The Promise () constructor takes a function as an argument. The function also accepts two functions resolve () and reject ().

If the promise returns successfully, the resolve () function is called. And, if an error occurs, the reject () function is called.

In the above program, a Promise object is created that takes two functions: resolve() and reject(). resolve () is used if the process is successful and reject () is used when an error occurs in the promise.

The promise is resolved if the value of count is true.

Working of JavaScript Promise methods —

JavaScript Promise Chaining: -

Promises are useful when we have to handle more than one asynchronous task, one after another. For that, we use promise chaining.

You can perform an operation after a promise is resolved using methods then (), catch () and finally ().

JavaScript then () method

The then () method is used with the callback when the promise is successfully fulfilled or resolved.

The syntax of then () method is:

promiseObject.then(on Fulfilled, on Rejected).

Example 2: Chaining the Promise with then ()

// returns a promise
let countValue = new Promise (function (resolve, reject) {
resolve ("Promise resolved").
});

// executes when promise is resolved successfully

countValue
. then (function successValue(result) {
console.log(result);
})

. then (function successValue1() {
console.log ("You can call multiple functions this way.") ;
});

Output

Promise resolved
You can call multiple functions this way.

In the above program, the then () method is used to chain the functions to the promise. The then () method is called when the promise is resolved successfully.

You can chain multiple then () methods with the promise.

Create an asynchronous function — with the return type as Promise -resolve and reject — (function is resolved — when it returns the even numbers, and it throws error — if the function throws an error message).

Examples of multiple — then call — Promise chaining

In this example, we can see that we are able to call multiple then statements to call the same function with different parameters.

JavaScript catch () method

The catch () method is used with the callback when the promise is rejected or if an error occurs. For example,

// returns a promise
let countValue = new Promise (function (resolve, reject) {
reject (‘Promise rejected’);
});

// executes when promise is resolved successfully
countValue.then(
function successValue(result) {
console.log(result);
},
)

// executes if there is an error
. catch (
function error Value(result) {
console.log(result);
}
);

In the above program, the promise is rejected. And the catch () method is used with a promise to handle the error.

Work of Promise chaining: -

JavaScript Promise Versus Callback

Promises are like callback functions in a sense that they both can be used to handle asynchronous tasks.

JavaScript callback functions can also be used to perform synchronous tasks.

Their differences can be summarized in the following points:

JavaScript Promise

  1. The syntax is user-friendly and easy to read.
  2. Error handling is easier to manage.

JavaScript finally () method

You can also use the finally () method with promises. The finally () method gets executed when the promise is either resolved successfully or rejected. For example,

Output: -

Conclusion: -

Promises in JavaScript is especially useful way to handle asynchronous calls in Java Script. Promises are like callback functions in a sense that they both can be used to handle asynchronous tasks.

In the next blog — we will learn about various methods available inside Promise object — which makes the handling of Asynchronous calls in the Java Script much easier.

Kumar Jitendra

An automation engineer.. enthusiastic about learning and exploring new things .