2016-06-21 82 views
3

我的代码会检查word.statusId以查看它是否脏。如果是,那么它会更新单词,然后如果这样做会更新wordForms。如果它干净,那么它只是更新wordForms。有人能告诉我,这是否是正确处理一个接一个承诺的正确方法?如何排序两个函数的运行并返回promise?

update =(): ng.IPromise<any> => { 
     var self = this; 
     if (self.word.statusId != 3) { 
      return self.wordEditSubmit() 
       .then(() => { 
        return self.wordFormCheckAndUpdate(); 
       }) 
     } else { 
      return self.wordFormCheckAndUpdate(); 
     } 
    } 

回答

4

你所描述的理想行为的确是实际行为。

当您使用arrow functions你并不需要保存的this值:再次使用简化的一个ternary condition

update =(): ng.IPromise<any> => { 
    if (this.word.statusId != 3) { 
    return this.wordEditSubmit() 
     .then(() => this.wordFormCheckAndUpdate()) 
    } else { 
    return this.wordFormCheckAndUpdate(); 
    } 
} 

update =(): ng.IPromise<any> => { 
    return this.word.statusId != 3 
    ? this.wordEditSubmit().then(() => this.wordFormCheckAndUpdate()) 
    : this.wordFormCheckAndUpdate(); 
} 
相关问题