2017-10-17 2 views
0

기본적으로 전체 변환 돈을 담당하는 큰 개체가 있습니다.개체 내 콜백에서 한 메서드에서 두 번째 값 반환

이 개체에는 4 가지 방법이 있습니다.

addTaxAndShowBack()은 내 "기본"방법이며 다른 종류의 콜백 지옥과 함께 체인을 실행합니다.

addTaxAndShowBack: function(priceField,selectedCurrency) { 
    var that = this; 
    var convertedToUSD = this.convertToUSD(priceField,selectedCurrency) 
     .then(function(response) { 
      console.log(response); 
      var priceInUSD = response; 
      that.addTax(priceInUSD,selectedCurrency) 
       .then(function (response) { 

        console.log(response); // !!! THIS CONSOLE.LOG DOESN'T LOG ANYTHING 

       }, function() { 
        console.log('error'); 
       }); 

     }, function (response) { 
      console.log(response); 
     }); 
}, 

처음 실행 된 방법 (convertedToUSD())은 정상적으로 통화 중 변환 된 금액을 사용자 기본 통화에서 USD로 반환합니다. 두 번째 것은 addTax()이고 가치를 반환하지 않습니다. console.log(response)은 아무 것도 기록하지 않습니다. 아마 addTax() 제대로 반환하지 않습니다 또는 내가 모르는 addTaxAndShowBack()에 제대로 지정하지에서 뭔가를 잘못하고 있어요

addTax: function(priceInUSD, selectedCurrency) { 
    var finalPriceInUSD; 
    if(priceInUSD<300){ 
     // i should also store userPriceInUSD in some variable 
     // maybe rootScope to send it to backend 
     finalPriceInUSD = priceInUSD*1.05; 
     console.log('after tax 5%: '+finalPriceInUSD); 
     return finalPriceInUSD; 
    } else { 
     finalPriceInUSD = priceInUSD*1.03; 
     console.log('after tax 3%: '+finalPriceInUSD); 
     return finalPriceInUSD; 
    } 
}, 

나는 당신의 도움이 필요 이유입니다 : addTax 방법의 코드입니다.

return finalPriceInUSD; 이것은 response에서 addTaxAndShowBack()의 두 번째 콜백이어야합니다.

답변

1

약속을 반환하지 않았습니다. 사용해보기

addTax: function(priceInUSD, selectedCurrency) { 
    var finalPriceInUSD; 
    if(priceInUSD<300){ 
     // i should also store userPriceInUSD in some variable 
     // maybe rootScope to send it to backend 
     finalPriceInUSD = priceInUSD*1.05; 
     console.log('after tax 5%: '+finalPriceInUSD); 
     return new Promise(res => { res(finalPriceInUSD) }); 
    } else { 
     finalPriceInUSD = priceInUSD*1.03; 
     console.log('after tax 3%: '+finalPriceInUSD); 
     return new Promise(res => { res(finalPriceInUSD) }); 
    } 
}, 
관련 문제