Why is a Promise an Object in JavaScript?

Muskan KumariMuskan Kumari
3 min read

So first of all, what is an object??

Suppose you have a notebook filled with questions and answers. That’s exactly what an object is!

An object is a collection of key-value pairs, which can include methods.

Assume that the key is the question and the value is its answer. Let’s take an example:

const Student = {
name : "Anuj",
age : 20,
hasEaten: false,
sayHello: function(){
console.log("Hi, I'm " + this.name);
}
 }
console.log(Student.age);

Here Student is the object name. name, age and hasEaten are keys whereas Anuj , 20 and false are values of those keys. So there are a total of 3 keys and 3 values, hence 3 pairs of keys and values.

Now what is a method?

In simple words, a method is a function in an object. In the above example, sayHello is the method key of the object while function(){…} is the value.

In an object, this keyword is used to call for a specific key followed by the dot operator . and then the key name.

Now that we’ve cleared what an object is let’s see what is a constructor function.

In simple words, a constructor function is like a blueprint for an object. It is like a recyclable object which can be used again and again at different instances.

For example:

function Student(Name, Age){
this.name = Name;
this.age = Age;
}

Here Student is a constructor function which takes name and age as parameters. Here, this.name is an object property which can be seen as a key and Name is the input value which was passed as a parameter.

How is this a blueprint?

const Harry = new Student("Harry",20);
const Hermione = new Student("Hermione",21);
const Ron = new Student("Ronald",22);

Here Student constructor function was called thrice and arguments were passed into it. The constructor function was then stored in a variable. Here Student worked as a blueprint, as everytime another object did not have to be created. (That's exactly what a blueprint does!)

Note: new + Constructor function creates an empty object.

const motherPromise = new Promise(function(resolve,reject){

    setTimeout(function(){

    const boughtIceCream = true;

    if(boughtIceCream){
        resolve("yes, bought woohoo");
    }
    else{
        reject("no,forgot :(");
    }

    }, 1000);

});

motherPromise
 .then(function(result){
    console.log("Resolved:", result);
 })
 .catch(function(error){
    console.log("Rejected: ", error);
 });

For beginners this code might look too much to take in at once. But don’t worry.

Our main aim is to know why promise is an object. Just look at the first line for now.

Here Promise is an inbuilt-constructor function in JavaScript. So new Promise creates a new empty promise object.

For now, you don’t have to know what the code means.

Just like in an object, there are key-value pairs and methods, a promise has internal states which can be seen as the keys. The result of these internal states can be seen as the values of this promise object. Methods which are used to access the functions that take these results as arguments.

Here, resolve and reject are internal states. result and error are the results of those internal states. .then() and .catch() are methods used to access the functions that take result and error as arguments.

So, to conclude:

Why is a promise an object?

A promise is an object because:

  1. new keyword + constructor function (Promise) is used to declare an object.

  2. It has the properties of objects. Key-value pairs, and methods.

  3. Internal states(resolve, reject) are keys, result of these internal states (result and error) are values, methods (.then(), .catch() ) are also there.

0
Subscribe to my newsletter

Read articles from Muskan Kumari directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Muskan Kumari
Muskan Kumari

-> UI/UX Designer -> Developer -> Relies heavily on caffeine. -> Learning lots of things at once.