0

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!

2
  • Off topic, but this construct looks weird to me: financeDocumentService.save(receipt, callback).$promise What is financeDocumentService? What does financeDocumentService.save() return? What data does the $promise resolve to? Why do you need to memorize receipt = response in the callback, just to be able to return receipt in the seemingly corresponding $promise.then()? I would have expected that the promise resolves to the same value you got in the callback. Commented Jul 9, 2017 at 18:31
  • Hi @Thomas, financeDocumentService is a factory for all finance documents, in example (Receipt, Credit Note, Debit Note, Invoice, etc). FinanceDocumentService.save() needs minimun data (id's) to save a document but returns a complete (refilled) object instance from database. Please, feel free to tell me if you see anything else weird. Thanks a lot for your time! Commented Jul 9, 2017 at 19:02

1 Answer 1

1

First return the promise:

$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()
    };

    //RETURN the promise
    ͟r͟e͟t͟u͟r͟n͟  financeDocumentService.save(receipt, function (response) {
        receipt = response;
        $uibModalInstance.close(response);
    }).$promise.then(function (data) {
        return receipt;
    });
};

Then chain from the promise:

$scope.saveAndPrint = function() {
    var promise = $scope.save();     
    promise.then(function(receipt) {                    
        $window.location.href = "#/finance/receipt_show/"+document.id;
    });
};

For more information,

Sign up to request clarification or add additional context in comments.

3 Comments

how did you underline return?
Thanks a lot @georgeawg!!
@baao String.fromCharCode(863,114,863,101,863,116,863,117,863,114,863,110,863) or console.log("͟r͟e͟t͟u͟r͟n͟".split(""))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.