2017-10-17 9 views
0

私は基本的にコンバージョンマネー全体を担当する大きなオブジェクトを持っています。オブジェクト内のコールバックで1つのメソッドから2番目に値を返す

このオブジェクトには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())は、米ドルへのユーザのデフォルトの通貨から変換されたお金を返す正常に動作しています。 2番目は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;これは、responseaddTaxAndShowBack()の2番目のコールバックである必要があります。

答えて

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) }); 
    } 
}, 
関連する問題