Reputation: 32848
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?
Upvotes: 0
Views: 64
Reputation: 121
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;
});
Upvotes: 3