Janani NP
YavarTechWorks
Published in
3 min readAug 30, 2022

--

A Quick Guide to Javascript Promise

A promise represents a value that is unknown now that may become known in the future. In other words, an asynchronous value that can be handled in the future. Before getting into promises in javascript let's understand what is synchronous and asynchronous.

The asynchronous requests can perform long network requests without blocking the main thread. For example, a waiter takes multiple orders in a restaurant. While the synchronous requests can only process one at a time, other requests wait until the first one completes. For example, a queue in a bank follows a token system where one person's request is completed and then moved on to another.

const p1 = new Promise((resolve , reject) => {

resolve(“success”)

});

const p2 = new Promise((resolve,reject) => {

reject(“Failure”)

})

p1.then(console.log)

p2.catch(console.log)

In the above example, the resolve is used to fulfill a request and throws a success message whereas the reject is used to reject a request and throw an error message. Promise resolve 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. The catch() method returns a promise and deals with rejected cases only.

Promises are used for asynchronous operations, i.e. non-blocking queues. The concept of promise in javascript was introduced in the 6th version of javascript ES6 in order to replace the concept of callbacks. Let's see the difference between the callback function and a promise in the following.

A callback function:

const f1 = (val,callback) => {

return callback(val + 10)

}

const callback = (val){

return val * 5

}

console.log(f1(1,callback))

//output — 55

//Similar function using promise

const p1 = val => new Promise ((resolve,reject) => {

resolve(val + 10)

})

p1(1).then(x => x*5).then(console.log)

//output — 55

In the above example, the output remains the same but you can see the code has been simplified while using promise. In the case of a single callback, it might look simple but when we tend to use multiple callbacks the code will look more clumsy and complex. So in order to simplify the concept of callback, the promise object was brought into the picture.

A promise is one of the following states.

  1. Pending -initial state, a promise is created and the result is undefined.
  2. Fulfilled -When a promise is resolved it is in the fulfilled state.
  3. Rejected -is the last stage in which a promise is failed to be executed.
States of Promise

Advantage of Promise :

  1. Improves Code Readability.
  2. Better handling of asynchronous operations.
  3. Better flow of control definition in asynchronous logic.
  4. Better Error Handling.
  5. Promise chaining => “then” can be used for chaining one result to produce another result concurrently. Similarly, multiple chains can be created.
  6. Better Error Handling.

--

--