Javascript 从承诺中返回数据并用另一种方法获取
关于如何使用async/await,我遗漏了一些东西,可能还有一些方法。 以下是我试图做的: 登录表单组件.htmlJavascript 从承诺中返回数据并用另一种方法获取,javascript,html,angular,ionic-framework,es6-promise,Javascript,Html,Angular,Ionic Framework,Es6 Promise,关于如何使用async/await,我遗漏了一些东西,可能还有一些方法。 以下是我试图做的: 登录表单组件.html <button (click)="sinInWithFacebook()">Sign In with Facebook</button> 认证服务 signInWithFacebook() { try { this.fb.login(['email']) .then((loginResponse: FacebookLoginRes
<button (click)="sinInWithFacebook()">Sign In with Facebook</button>
认证服务
signInWithFacebook() {
try {
this.fb.login(['email'])
.then((loginResponse: FacebookLoginResponse) => {
const credential = firebase.auth.FacebookAuthProvider.credential(loginResponse.authResponse.accessToken);
return <LoginResponse>{
result: this.auth.auth.signInWithCredential(credential)
}
})
}
catch (e) {
return {
error: e
}
}
signInWithFacebook(){
试一试{
this.fb.login(['email'])
.然后((loginResponse:FacebookLoginResponse)=>{
const credential=firebase.auth.FacebookAuthProvider.credential(loginResponse.authResponse.accessToken);
返回{
结果:此.auth.auth.signInWithCredential(凭证)
}
})
}
捕获(e){
返回{
错误:e
}
}
}
loginResponse
当我希望它返回结果
对象时,它将始终返回未定义
。我相信这与异步方法有关。你能帮我吗?
谢谢您应该使用Facebook功能从
登录返回结果:
try {
return this.fb.login(['email'])
.then((loginResponse: FacebookLoginResponse) => {
const credential = firebase.auth.FacebookAuthProvider.credential(loginResponse.authResponse.accessToken);
return <LoginResponse>{
result: this.auth.auth.signInWithCredential(credential)
}
})
}
试试看{
返回此.fb.login(['email'])
.然后((loginResponse:FacebookLoginResponse)=>{
const credential=firebase.auth.FacebookAuthProvider.credential(loginResponse.authResponse.accessToken);
返回{
结果:此.auth.auth.signInWithCredential(凭证)
}
})
}
您应该使用Facebook的登录功能返回结果:
try {
return this.fb.login(['email'])
.then((loginResponse: FacebookLoginResponse) => {
const credential = firebase.auth.FacebookAuthProvider.credential(loginResponse.authResponse.accessToken);
return <LoginResponse>{
result: this.auth.auth.signInWithCredential(credential)
}
})
}
试试看{
返回此.fb.login(['email'])
.然后((loginResponse:FacebookLoginResponse)=>{
const credential=firebase.auth.FacebookAuthProvider.credential(loginResponse.authResponse.accessToken);
返回{
结果:此.auth.auth.signInWithCredential(凭证)
}
})
}
函数不会返回任何内容。然后,试试。。catch
block不适用于承诺
signInWithFacebook():Promise<LoginResponse> {
return this.fb.login(['email'])
.then((loginResponse: FacebookLoginResponse) => {
const credential = firebase.auth.FacebookAuthProvider.credential(loginResponse.authResponse.accessToken);
//my guts tell me that `this.auth.auth.signInWithCredential` is async as well.
//so let the promise chain deal with that/resolve that
return this.auth.auth.signInWithCredential(credential);
})
.then(
result => ({ result }),
error => ({ error })
);
}
signInWithFacebook():承诺{
返回此.fb.login(['email'])
.然后((loginResponse:FacebookLoginResponse)=>{
const credential=firebase.auth.FacebookAuthProvider.credential(loginResponse.authResponse.accessToken);
//我的直觉告诉我'this.auth.auth.signiWithCredential'也是异步的。
//因此,让承诺链来处理/解决这个问题
返回此.auth.auth.signiWithCredential(凭证)的.auth.auth.signiWithCredential;
})
.那么(
结果=>({result}),
error=>({error})
);
}
函数不会返回任何内容。然后,试试。。catch
block不适用于承诺
signInWithFacebook():Promise<LoginResponse> {
return this.fb.login(['email'])
.then((loginResponse: FacebookLoginResponse) => {
const credential = firebase.auth.FacebookAuthProvider.credential(loginResponse.authResponse.accessToken);
//my guts tell me that `this.auth.auth.signInWithCredential` is async as well.
//so let the promise chain deal with that/resolve that
return this.auth.auth.signInWithCredential(credential);
})
.then(
result => ({ result }),
error => ({ error })
);
}
signInWithFacebook():承诺{
返回此.fb.login(['email'])
.然后((loginResponse:FacebookLoginResponse)=>{
const credential=firebase.auth.FacebookAuthProvider.credential(loginResponse.authResponse.accessToken);
//我的直觉告诉我'this.auth.auth.signiWithCredential'也是异步的。
//因此,让承诺链来处理/解决这个问题
返回此.auth.auth.signiWithCredential(凭证)的.auth.auth.signiWithCredential;
})
.那么(
结果=>({result}),
error=>({error})
);
}
因为用Facebook登录实际上什么也不返回。为什么?该返回不应该起作用吗?不。该返回返回到其父承诺链,而不是返回到调用signInWithFacebook()
的函数。为了使其工作,您还可以添加return this.fb.login(['email'])…
。如果您已经在使用wait
,为什么要使用Then
Then?;)xxx=等待这个.fb.login(…)
etcbecausesignInWithFacebook
实际上什么也不返回。为什么?该返回不应该起作用吗?不。该返回返回到其父承诺链,而不是返回到调用signInWithFacebook()
的函数。为了使其工作,您还可以添加return this.fb.login(['email'])…
。如果您已经在使用wait
,为什么要使用Then
Then?;)xxx=等待这个.fb.login(…)
etc