2017-07-25 283 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);})  
}