I have big object which basically is responsible for whole converting money.
Within this object I've got 4 methods.
addTaxAndShowBack()
is my " main " method and it executes others as a chain with some kind of callback hell.
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);
});
},
First executed method ( convertedToUSD()
) is working fine it returns converted money from user default currency to USD. Second one is addTax()
and it doesn't return value how I would like to. The console.log(response)
doesn't log anything. The code of addTax
method is:
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;
}
},
I'm probably doing something wrong in addTax()
not returning correctly or not assigning it correctly in addTaxAndShowBack()
I don't know and that's why I need your help.
return finalPriceInUSD;
this is what response
in addTaxAndShowBack()
in second callback should be.
You are not returning a promise. Try this
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) });
}
},