I'm trying to solve how I can to chain several js functions (two in this case). I have one javascript function to save() an another for saveAndPrint(). I need to chain the call because I need the id generated in save() to forward it for printing in the second one. Probably I just could use only one function to do both things but I'd like to learn how to do this. At the moment I just getting an 'undefined' because second function starts before the first one finishes. Here is the code:
$scope.save = function () {
var receipt = {
documentType: 'RCI',
expirationDate: new moment().format('YYYY-MM-DD'),
person: {id: $scope.financeDocuments[0].person.id},
payments: getPayments(),
promotions: [],
creationDate: new moment().format('YYYY-MM-DD'),
compensatedDocuments: getDocumentsToPay()
};
financeDocumentService.save(receipt, function (response) {
receipt = response;
$uibModalInstance.close(response);
}).$promise.then(function (data) {
return receipt;
});
};
$scope.saveAndPrint = function() {
var document = $scope.save();
$window.location.href = "#/finance/receipt_show/"+document.id;
};
Thanks a lot!
financeDocumentService.save(receipt, callback).$promiseWhat isfinanceDocumentService? What doesfinanceDocumentService.save()return? Whatdatadoes the$promiseresolve to? Why do you need to memorizereceipt = responsein the callback, just to be able toreturn receiptin the seemingly corresponding$promise.then()? I would have expected that the promise resolves to the same value you got in the callback.