0

I have this Typescript / Javascript function:

wordFormRowClicked = (wf): ng.IPromise<any> => {
    var self = this;
    if (this.wordFormIdentity != wf.wordFormIdentity) {
        angular.forEach(self.word.wordForms, function (wf, key) {
            var wordFormNgForm = 'wordFormNgForm_' + wf.wordFormIdentity;
            if (self[wordFormNgForm].$pristine) {
                ;
            } else {
                self.wordFormUpdate(wf).then((): any => {
                    self[wordFormNgForm].$setPristine();
                });
            }
        });
        this.wordFormIdentity = wf.wordFormIdentity;
    }
}

self.wordFormUpdate(wf) returns a promise and so gives the correct return types but if wordFormUpdate is not called then it does not return a promise and also there is a possibility that multiple wordFormUpdates will be called and I have to ensure they have all finished before returning.

Can anyone suggest how I can do this?

1 Answer 1

3

Your wordFormRowClicked function must return a promise:

var _this = this;
wordFormRowClicked = function (wf) {
    var self = _this;
    var promises = [];
    if (_this.wordFormIdentity != wf.wordFormIdentity) {
        angular.forEach(self.word.wordForms, function (wf, key) {
            var wordFormNgForm = 'wordFormNgForm_' + wf.wordFormIdentity;
            if (self[wordFormNgForm].$pristine) {
                ;
            }
            else {
                var updatePromise = self.wordFormUpdate(wf).then(function () {
                    return self[wordFormNgForm].$setPristine();
                });
                promises.push(updatePromise);
            }
        });

    }
    return $q.all(promises);
};
wordFormRowClicked.then(function () {
    //at this point all promises are resolved
    this.wordFormIdentity = wf.wordFormIdentity;
});
Sign up to request clarification or add additional context in comments.

Comments

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.