如何从简单的函数返回AngularJS承诺?

时间:2016-05-17 14:45:44

标签: angularjs

我的代码是这样做的:

wordFormRowClicked = (wf): ng.IPromise<any> => {
    var self = this;
    if (this.wordFormIdentity != wf.wordFormIdentity) {
        return self.wordFormUpdate(wf).then(() => {
            this.wordFormIdentity = wf.wordFormIdentity;
        })
    }
}

self.wordFormUpdate返回一个promise但是当if条件不匹配时,则不返回promise。

在这种情况下我如何回复承诺?

2 个答案:

答案 0 :(得分:2)

假设wordFormUpdate是promise:

wordFormRowClicked = (wf): ng.IPromise<any> => {
    var self = this;
    if (this.wordFormIdentity != wf.wordFormIdentity) {
        return self.wordFormUpdate(wf);
    }
}

您可以将您的功能运行为:

wordFormRowClicked(someParam).then(() => {
    this.wordFormIdentity = wf.wordFormIdentity;
});

答案 1 :(得分:1)

只需创建一个承诺并在函数末尾返回它

if (...) { 
    return ...
}
return new Promise(...)