Sitemap
Towards Dev

A publication for sharing projects, ideas, codes, and new theories.

JavaScript Promises Demystified: Mastering Asynchronous Code with Ease

6 min readOct 27, 2022

--

Press enter or click to view image in full size

What is a Promise ?

The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.

In other words,

A Promise is a special JavaScript object. It produces a value after an asynchronous operation completes successfully, or an error if it does not complete successfully.

The word ‘asynchronous’ means that something happens in the future, not right now.

Promise Constructor

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

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

The executor function takes two arguments, resolve and reject.

A promise object has the following internal properties:

  1. state – This property can have the following values:
  • pending: Initially when the executor function starts the execution.
  • fulfilled: When the promise is resolved.
  • rejected: When the promise is rejected.

--

--

Eishta | @ExpediaGroup | SDE 3
Eishta | @ExpediaGroup | SDE 3

Written by Eishta | @ExpediaGroup | SDE 3

Software Engineer passionate about Frontend Engineering

Towards Dev
Towards Dev

Published in Towards Dev

A publication for sharing projects, ideas, codes, and new theories.