Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从承诺中返回数据并用另一种方法获取_Javascript_Html_Angular_Ionic Framework_Es6 Promise - Fatal编程技术网

Javascript 从承诺中返回数据并用另一种方法获取

Javascript 从承诺中返回数据并用另一种方法获取,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

关于如何使用async/await,我遗漏了一些东西,可能还有一些方法。 以下是我试图做的:

登录表单组件.html

<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(…)
etcbecause
signInWithFacebook
实际上什么也不返回。为什么?该返回不应该起作用吗?不。该返回返回到其父承诺链,而不是返回到调用
signInWithFacebook()
的函数。为了使其工作,您还可以添加
return this.fb.login(['email'])…
。如果您已经在使用
wait
,为什么要使用
Then
Then?;)
xxx=等待这个.fb.login(…)
etc