简体   繁体   English

导入异步 function 的 Async Await 未正确等待

[英]Async Await of imported asynchronous function not waiting correctly

I'm flummoxed.我很困惑。

I have created an asynchronous utility function that I am exporting from one file and importing into a React Class Component.The function makes an API call to an authentication service which returns an object. I have created an asynchronous utility function that I am exporting from one file and importing into a React Class Component.The function makes an API call to an authentication service which returns an object.

My goal is to be able to call that function in my component and set the returned object equal to a variable called "tokens" which I can then further manipulate.我的目标是能够在我的组件中调用 function 并将返回的 object 设置为一个名为“令牌”的变量,然后我可以进一步操作该变量。

My problem is that no matter how I seem to format this i'm unable to get the function in the component to wait for the return of the async function before moving on.我的问题是,无论我如何格式化它,我都无法在组件中获取 function 以等待异步 function 返回,然后再继续。

Here is a simplified version of the utility function:这是实用程序 function 的简化版本:

// configure and send API request using Axios library
export const retrieveTokens = async () => {
  
  // configure request
  var config = {
    // configs
  };

  // send request
  axios(config)
    // handle response
    .then(function (response) {
      let cognitoTokens = response.data;
      console.log("A:")
      console.log(cognitoTokens);
      return cognitoTokens;
    })
    // handle error
    .catch(function (error) {
      console.log(error);
    });
};

and then this utility function is being imported into the React Class Component like so:然后这个实用程序 function 被导入到 React Class 组件中,如下所示:

import React, { Component } from "react";
import { retrieveTokens } from "./utils.js";

class HeaderLogic extends Component {
  componentDidMount() {
    async function authenticationFlow() {
      let tokens = await retrieveTokens();
      console.log("B:");
      console.log(tokens);

    }
    authenticationFlow();
  }

  render() {
    return <></>;
  }
}

export default HeaderLogic;

My expected result would be:我的预期结果是:

A:
{Tokens}

B:
{Tokens}

but I am only able to get但我只能得到

B:
Undefined

A:
{Tokens}

I have tried various different syntax's and I simply cannot get the console logs in authenticationFlow() to await!我尝试了各种不同的语法,但我根本无法让控制台登录authenticationFlow()等待!

You forgot to return .你忘了return Also, don't declare a function as async if you don't have any await inside it (not that it causes any problem but it's just useless):另外,如果 function 内部没有任何await ,请不要将其声明为async (并不是说它会导致任何问题,但它只是没用):

// configure and send API request using Axios library
export const retrieveTokens = () => {
  
  // configure request
  var config = {
    // configs
  };

  // send request
  return axios(config)
    // handle response
    .then(function (response) {
      let cognitoTokens = response.data;
      console.log("A:")
      console.log(cognitoTokens);
      return cognitoTokens;
    })
    // handle error
    .catch(function (error) {
      console.log(error);
    });
};

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

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