I'm new to writing and using promises and I'd like some advice. I have to chain my promises because some functions can only be run after others. I did used to handle this with a LOT of callback functions, which looked very messy and confusing. But with the chaining I'm doing... it's starting to look a bit messy again and I'm wondering whether I'm doing this correctly...
function calcNetPriceSaleCharge(theItem) {
var setInitialChargeAmount = miscSaleSvc.getInitialCharge(theItem);
var setDiscountAmount = miscSaleSvc.getDiscountAmount(theItem);
$q
.all([setInitialChargeAmount, setDiscountAmount])
.then(function(values) {
theItem.initialchargeamount = values[0];
theItem.initialdiscountamount = values[1];
})
.then(function() {
var setActualCharge = miscSaleSvc.getActualCharge(theItem);
var setVat = miscSaleSvc.setVat(theItem);
$q
.all([setActualCharge, setVat])
.then(function(values) {
theItem.actualcharge = values[0];
theItem.vat = values[1];
})
.then(function() {
var setTotal = miscSaleSvc.getSaleTotal(theItem);
$q
.all([setTotal])
.then(function(values) {
theItem.total = values[0];
})
.catch(function(error) {
console.log(error);
});
})
.catch(function(error) {
console.log(error);
});
})
.catch(function(error) {
console.log(error);
});
}
this does work but im not sure I'm going about this the right way! An example function that is being called is this...
srv.getInitialCharge = function(theItem) {
//set up the deferred var
var deferred = $q.defer();
var initialchargeamount = parseFloat(theItem.normalperiodcharge * theItem.quantity);
if (isNaN(initialchargeamount)) {
deferred.reject("Error when calculating Initial Charge Amount.");
} else {
//set up the failed result
deferred.resolve(initialchargeamount);
}
//return the promise
return deferred.promise;
};
Thanks in advance for your help :)
You created a little callback hell which is exactly what Promises try to avoid. Remeber that you can also return a Promise inside then
block to handle it further with then
in the same chain of calls:
function calcNetPriceSaleCharge(theItem) {
var setInitialChargeAmount = miscSaleSvc.getInitialCharge(theItem);
var setDiscountAmount = miscSaleSvc.getDiscountAmount(theItem);
$q.all([setInitialChargeAmount, setDiscountAmount])
.then(function(values) {
theItem.initialchargeamount = values[0];
theItem.initialdiscountamount = values[1];
})
.then(function() {
var setActualCharge = miscSaleSvc.getActualCharge(theItem);
var setVat = miscSaleSvc.setVat(theItem);
return $q.all([setActualCharge, setVat]);
})
.then(function(values) {
theItem.actualcharge = values[0];
theItem.vat = values[1];
})
.then(function() {
var setTotal = miscSaleSvc.getSaleTotal(theItem);
return $q.all([setTotal]);
})
.then(function(values) {
theItem.total = values[0];
})
.catch(function(error) {
console.log(error);
});
}
another variation:
function calcNetPriceSaleCharge(theItem) {
var setInitialChargeAmount = miscSaleSvc.getInitialCharge(theItem);
var setDiscountAmount = miscSaleSvc.getDiscountAmount(theItem);
return $q
.all([setInitialChargeAmount, setDiscountAmount])
.then(function(values) {
theItem.initialchargeamount = values[0];
theItem.initialdiscountamount = values[1];
})
.then(function() {
var setActualCharge = miscSaleSvc.getActualCharge(theItem);
var setVat = miscSaleSvc.setVat(theItem);
return $q.all([setActualCharge, setVat]);
})
.then(function(values) {
theItem.actualcharge = values[0];
theItem.vat = values[1];
})
.then(function() {
var setTotal = miscSaleSvc.getSaleTotal(theItem);
return $q.all([setTotal]);
})
.then(function(values) {
return (theItem.total = values[0]);
});
}
calcNetPriceSaleCharge(something)
.then(function(finalValue) {
console.log(finalValue);
})
.catch(function(error) {
console.log(error);
});
Just for completeness sake a version using async/await
syntax (Note, that this might not be available for older browsers ). For better readability the variable naming should be changed, however.
async function calcNetPriceSaleCharge(theItem) {
try {
const setInitialChargeAmount = miscSaleSvc.getInitialCharge(theItem),
setDiscountAmount = miscSaleSvc.getDiscountAmount(theItem);
const values0 = await Promise.all( [setInitialChargeAmount, setDiscountAmount] );
theItem.initialchargeamount = values0[0];
theItem.initialdiscountamount = values0[1];
const setActualCharge = miscSaleSvc.getActualCharge(theItem),
setVat = miscSaleSvc.setVat(theItem);
const values1 = await Promise.all( [setActualCharge, setVat] );
theItem.actualcharge = values1[0];
theItem.vat = values1[1];
const values2 = await miscSaleSvc.getSaleTotal(theItem);
theItem.total = values2;
} catch ( e ) {
console.log( e );
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.