2016-06-21 77 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(); 
} 
相關問題