0

I was going through a tutorial about Promises in Javascript. I saw usages of then() method in many places.

When I wrote the below code, I saw the "then()" function under __proto__ section of the console.

const myPromise = new Promise(function(resolve, reject) {});
console.log(myPromise);

But I am not able to observe the same "then()" function when I wrote the below code,

class Car {
   constructor(color, type, doors) {
      this.color = color;
      this.type = type;
      this.doors = doors
   }
}
const myCar = new Car('blue', 'sedan', '4');
console.log(myCar);

So , I was thinking that , Can we create our own then() function in Javascript and execute it?.

user3742125
  • 557
  • 2
  • 6
  • 26
  • 2
    Have you read anything about promises? `.then()` is a method on a promise object. You can it to register a resolve handler or a reject handler which will be called by the promise infrastructure when that promise is resolved or rejected. It's not a generic method, it's something specific to promise objects. – jfriend00 Apr 16 '19 at 06:05
  • Appends fulfillment and rejection handlers to the promise, and returns a new promise resolving to the return value of the called handler, or to its original settled value if the promise was not handled (i.e. if the relevant handler onFulfilled or onRejected is not a function). – Prasanna Brabourame Apr 16 '19 at 06:06
  • @Syed Mehtab Hassan , it does not explain what does then() do , when we can use it and from where it is coming. – user3742125 Apr 16 '19 at 06:07
  • @user3742125 i think it have see this answer https://stackoverflow.com/a/31453579/11046080 – Syed Mehtab Hassan Apr 16 '19 at 06:09
  • @Syed Mehtab Hassan , I have edited my question, Review it. – user3742125 Apr 16 '19 at 06:13
  • @user3742125 yeah because both are different 1st is promise and the second is object but i am not sure about creating a then function but what to you want by creating that ? – Syed Mehtab Hassan Apr 16 '19 at 06:21
  • @Syed Mehtab Hassan , remove your comment saying that my question is a duplicate of another question. – user3742125 Apr 16 '19 at 06:32
  • Your `Car` class does not have *any* method. Why would you expect it to have a `then` method? – Bergi Apr 16 '19 at 06:38
  • Possible duplicate of [What does the function then() mean in JavaScript?](https://stackoverflow.com/questions/3884281/what-does-the-function-then-mean-in-javascript) – ponury-kostek Apr 16 '19 at 14:31

2 Answers2

1

It is because when you create promise it's __proto__ points to Promise.prototype and when you create a object using class it's __proto__ points to Object.prototype

for more information read this https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain

and yes we can create our own then function and execute it but we should not do this unless you know exactly what you are doing

here is a example to create our own then method

class myObj extends Object {
  then() {
    console.log('then is called');
  }
}

var mo = new myObj()

and now you can call mo.then()

Tripurari Shankar
  • 2,806
  • 10
  • 21
1

Have a look at this blog post. You should have a pretty solid idea of what promises are all about when you are finished.

Andrew
  • 5,005
  • 1
  • 21
  • 40
  • ugh ... jQuery Promises, at least in 2.x, are not Promise/A+ compliant, and Q promises used to be even worse!! I'd skip chapters 2, 3, 4 and 5 altogether – Jaromanda X Apr 16 '19 at 08:31