2017-07-25 244 views
0

我已經寫了下面的函數功能 - 我想沒有包裝他們在一個自定義的承諾需要返回現有承諾的功能:Angular2承諾 - 返回已返回一個承諾

doAuthWithPrompt(): Promise <any> { 
     this.getUser() // returns promise 
     .then (user => { 
     if (user == undefined) { 
      this.promptForPassword() // returns promise 
      .then (data => { 
      return this.doAuth(data.email, data.password); // returns promise 
      }) 
     } 
     else { 
      return this.doAuth(user.email, user.password) // returns promise 
      }; 

     }) 
     .catch (e => {return Promise.reject(false);}) 

    } 

錯誤我在我的IDE(Visual Studio代碼)我得到的是:

[TS]聲明類型既不是「無效」,也沒有「任何」必須 返回一個值的函數。

我在定義doAuthWithPrompt時錯過了什麼?謝謝。

+1

你需要'返回this.getUser()then..' – echonax

+0

謝謝!爲什麼我不需要'return this.promptForPassword()'? – user1361529

+1

@echonax爲什麼不作爲回答發佈? – 0mpurdy

回答

1

您需要返回包裝Promise及其鏈還承諾:

doAuthWithPrompt(): Promise <any> { 
     return this.getUser() // returns promise 
     .then (user => { 
      if (user == undefined) { 
      return this.promptForPassword() // returns promise 
       .then (data => { 
       return this.doAuth(data.email, data.password); // returns promise 
       }) 
      } else { 
      return this.doAuth(user.email, user.password) // returns promise 
      }  
     }) 
     .catch (e => {return Promise.reject(false);})  
}