-1

I Have an object (myObject). For this object I created a method (objectPromise) which returns a Promise

function myObject(){
   this.number = 2;
   this.objectPromise = function(data) {
        return new Promise((resolve, reject) => { 
            if (data == this.number) {
               resolve();
            } else {
               reject();
            }
       });
   };
 };

then I have this code 1)

obj = new myObject();
myPromise1
.then(obj.objectPromise)
.then(function(result){

})
.catch(function(err){

});

2)

obj = new myObject();
myPromise1
.then(function(result){
   obj.objectPromise(result)
})
.then(function(result){

})
.catch(function(err){

});

I didn't understand why 1) doesn't call my Promise

Bruce_Wayne
  • 19
  • 1
  • 5
  • This is quite unclear; `objectPromise` is a function that returns a promise - is that what you intended? What are you trying to achieve here? – Oliver Charlesworth Dec 22 '17 at 19:23
  • Did you mean `then(obj.objectPromise)`? Anyways, your code is all over the place. For one, `this.data` doesn't exist. Also, you aren't returning anything to the other `then`s. – Andrew Li Dec 22 '17 at 19:25
  • Sorry I made I mistake, yes it returns a promise, but what I didn't understand is that objectPromise wasn't an Object method I wouldn't have to write my code like 2) – Bruce_Wayne Dec 22 '17 at 19:27
  • It's an instance method. – Andrew Li Dec 22 '17 at 19:28
  • @Li357 I fixed this.data and obj.objectPromise, sorry. what do you mean by it's an instance method ? What's the diference between `.then(obj.objectPromise)` and `.then(function(result) { obj.objectPromise(result) })` – Bruce_Wayne Dec 22 '17 at 19:34
  • Methods that use`this` are not "detachable". To specify your method as a .then callback, you need to bind in the method's host object - `.then(obj.method.bind(obj))`. That's JavaScript, regardless of what the method returns, promise or something else. – Roamer-1888 Dec 24 '17 at 09:26
  • @Roamer-1888 thank you that what I wanted to know , i tried it but I noticed if my object is filled during promises chaining I won’t get its value for example : `obj = {}; myPromise1.then(objectPromise.bind(obj))` If I add attributes to obj in myPromise1 I will get an the empty obj passed to objectPromise , do you know why ? Are Promises created statically ? – Bruce_Wayne Dec 24 '17 at 13:50
  • Not sure what you might be trying to achieve with `obj = {}; myPromise1.then(objectPromise.bind(obj));`. It's a nonsense because `objectPromise` doesn't exist except as a property of instances of `myObject()` and you have introduced a second thing named `obj`. – Roamer-1888 Dec 24 '17 at 15:20

1 Answers1

0

For 1), you are returning the function with this line .then(obj.objectPromise), not the promise itself, so it's actually never called.

Based on the 1) and 2), and if myPromise1 is returning result, If I understand the problem correctly, I think you are looking for something more along the lines of:

obj = new myObject();
myPromise1
  .then((result) => 
    obj.objectPromise(result)
      .then((res) => {
        console.log('objectPromise success code here')
      })
      .catch((err) => {
        console.log('objectPromise fail code here')
      }))
  .catch((err) => {
    console.log('myPromise1 fail code here')
  });
ReyHaynes
  • 2,932
  • 1
  • 15
  • 22