2017-12-22 16 views
-1

개체가 있습니다 (myObject). 이 객체 나 메소드 약속 반환 (objectPromise)를 생성약속을 방법으로 사용하는 방법은 무엇입니까?

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

이 코드 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를 가지고 왜 이해가 안되는가 1)는 내 약속을하지 않는다

+0

이것은 아주 불분명하다; 'objectPromise'는 약속을 되 돌리는 함수입니다 - 의도 한 것입니까? 여기서 무엇을 이루려고합니까? –

+0

그럼'(obj.objectPromise)'를 원하셨습니까? 어쨌든, 코드가 여기 저기에 있습니다. 하나는 'this.data'가 존재하지 않습니다. 또한, 당신은 다른 '그 다음'에 아무것도 반환하지 않습니다. – Li357

+0

죄송합니다. 실수로 만들었습니다. 예를 들어 약속을 되 돌렸지 만, 제가 이해하지 못했던 것은 objectPromise가 Object 메소드가 아니었기 때문에 제가 코드를 작성하지 않아도된다는 것입니다. 2) –

답변

0

1)의 경우,이 라인이 .then(obj.objectPromise) 인 함수가 반환되므로 실제로는 호출되지 않습니다. 을 바탕으로

1) 2) 및 myPromise1이 result를 반환하면 내가 정확하게 문제를 이해한다면, 나는 당신의 라인을 따라 더 뭔가를 찾고있다 생각 :

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') 
    });