简体   繁体   English

异步功能不等待

[英]Async function does not await

I am having a problem where an async function does not appear to be waiting. 我有一个异步函数似乎没有在等待的问题。 I am calling one async function from another, with the second returning a value after async operations have completed and then the first should be returning this as it has awaited it. 我正在从另一个调用一个异步函数,第二个在异步操作完成后返回一个值,然后第一个应在等待时返回该值。 But when logging accessToken in the first function it logs before awaiting the return from the second. 但是,当在第一个函数中记录accessToken ,它将在等待第二个函数返回之前进行记录。 Where am I going wrong? 我要去哪里错了? Thanks in advance. 提前致谢。

export const confirmCode = async (verificationId, code) => {
  try {
    const credential = await firebase.auth.PhoneAuthProvider.credential(verificationId, code);
    const accessToken = await authenticate(credential);
    console.log(accessToken); // prints undefined as does not wait for above function call?
    return accessToken;
  } catch (error) {
    console.log(error)
    // this.showMessageErrorByCode(e.error.code);
  }
}

const authenticate = async (credential) => {
  try {
    await firebase.auth().signInWithCredential(credential).then(result => {
      const user = result.user;
      user.getIdToken().then(accessToken => {
        return accessToken;
      });
    })
  } catch (error) {
    console.log(error);
  }
}

You should not mix async/await with the older version .then() . 您不应将async/await与旧版本.then()

Just use it without then() like so: 只需不使用then()就可以使用它,如下所示:

export const confirmCode = async (verificationId, code) => {
  try {
    const credential = await firebase.auth.PhoneAuthProvider.credential(verificationId, code);
    const accessToken = await authenticate(credential);
    console.log(accessToken); // prints undefined as does not wait for above function call?
    return accessToken;
  } catch (error) {
    console.log(error)
    // this.showMessageErrorByCode(e.error.code);
  }
}

const authenticate = async (credential) => {
  try {
    let result = await firebase.auth().signInWithCredential(credential); // <-- use await
    const user = result.user;
    accessToken = await user.getIdToken(); // <-- use await
    return accessToken;
  } catch (error) {
    console.log(error);
  }
}

For more detailed explanation, why your code does not work: 有关更详细的说明,为什么您的代码不起作用:

  • You are returning from within a .then() which is not possible 您是从.then()返回的.then()这是不可能的
  • If you would want to use the old way of writing async functions, you would need to use: 如果要使用编写异步函数的旧方法,则需要使用:
    • return new Promise((resolve, reject) => { /* Code ... */ }); to wrap your function content 包装您的功能内容
    • resolve(accessToken) instead of return resolve(accessToken)而不是返回
    • .then() and .catch() instead of await and try/catch .catch() .then().catch()而不是awaittry/catch
    • and some rejects where you can't resolve anything (so probably in the catch block) 还有一些拒绝您无法解决的问题(可能是在catch块中)

But I would suggest you to use the async/await approach, as it is easier to read. 但我建议您使用async/await方法,因为它更易于阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM