2017-10-17 168 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())工作正常返回轉換資金從用戶默認貨幣爲美元。第二個是addTax(),它沒有返回值我想如何。 console.log(response)不記錄任何東西。 addTax方法的代碼是:

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()無法正常返回或不正確addTaxAndShowBack()分配它,我不知道,這就是爲什麼我需要你的幫助。

return finalPriceInUSD;這就是responseaddTaxAndShowBack()中的第二個回調應該是。

回答

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) }); 
    } 
}, 
相關問題