我已经尝试了几个小时,但仍无法获得正确的流量。我将首先共享代码,然后再进行解释。

jobSearch();

const jobSearch = () => {
  return (dispatch) => {

  console.log('DEBUG::step 1:');

  if (!refreshToken()) {
    console.log('DEBUG::step 6:');
    //.......
    //Call function to print step 8, step 9

  } else {
    console.log('DEBUG::step 7:');
    //Perform other operation
  }
}


基本上,refreshToken()是一种对jwt进行解码以检查是否过期的方法,如果过期,则调用REST来检索新令牌,因此上面有一个网络请求,并且函数refreshToken将返回一个布尔值以指示整个刷新令牌流是成功还是失败。

const refreshToken = async () => {
  console.log('DEBUG::step 2:');
  let valid = true;

  if (!validateAccessToken()) { //<==just a flow to decode jwt, no async flow
    console.log('DEBUG::step 4:');

    // Configure retry operation
    const operation = retry.operation({
      retries: MAX_RETRIES_USER_LOGIN,
      factor: 1,
      minTimeout: INTERVAL_RETRY_USER_LOGIN,
      maxTimeout: INTERVAL_RETRY_USER_LOGIN
    });

    // Configure HTTP request
    const ax = axios.create({
      timeout: TIMEOUT_CONNECT,
      headers: {
        'Content-Type': 'application/json; charset=utf-8'
      },
      cancelToken: cancelTokenSourceJobSearch.token
    });

    console.log('DEBUG::hihi0:');
    await operation.attempt(() => {
      ax.post(urljoin(API_BASE_URL, API_ENDPOINT_TOKEN_REFRESH), {
        data: {
          refresh_token: global.setting.refresh_token
        }
      })
      .then(({ data }) => {
        valid = true;
        console.log('DEBUG::hihi1:');
        //SUCCESS!
      }).catch((err) => {
        console.log('DEBUG::hihi3:');

        // Log error to console
        console.log(err);

        if (axios.isCancel(err)) {
          valid = false;
          return;
        } else if (operation.retry(err)) {
          valid = false;
          return;
        }
      });
      return valid;
    });
  } else {
    console.log('DEBUG::step 5:');
    return valid;
  }
};


下面是打印的日志


  调试::步骤1:
  
  调试::步骤2:
  
  调试::步骤3:
  
  调试::步骤4:
  
  调试:: hihi0:
  
  调试::步骤7:
  
  调试:: hihi1:



Step 7为何在hihi1之前打印?我已经做到了async await
step 6未打印,所以refreshToken操作成功
hihi3未打印,因此也不例外


任何帮助将不胜感激!

更新!

正如@CertainPerformance和@briosheje所评论的:我已更新为以下内容:

jobSearch();

const jobSearch = () => {
  return async (dispatch) => { //<==HERE

  console.log('DEBUG::step 1:');

  const shouldRefreshToken = await refreshToken();//<==HERE
  if (!shouldRefreshToken) {//<===HERE
    console.log('DEBUG::step 6:');
    //.......
    //Call function to print step 8, step 9

  } else {
    console.log('DEBUG::step 7:');
    //Perform other operation
  }
}


然后流程更改成为异常,如下所示:


  调试::步骤1:
  
  调试::步骤2:
  
  调试::步骤3:
  
  调试::步骤4:
  
  调试:: hihi0:
  
  调试::步骤6:
  
  调试:: hihi1:

最佳答案

if (!refreshToken()) {
     ^------ this is async.. Which returns a Promise<boolean>, which is always truthy.


(如此处所述):

const refreshToken = async () // and some other stuff.


因此,由于将其标记为async,因此它将始终返回Promise,这始终会导致真实值。

由于它是异步的,因此您应该保留promise响应并对其进行评估:

console.log('DEBUG::step 1:');
// Store here the value of refreshToken
const shouldRefreshToken = await refreshToken();

if (!shouldRefreshToken) {
//  ^--- Syncronous flow here.
  console.log('DEBUG::step 6:');
  //.......

} else {
  console.log('DEBUG::step 7:');
  //Perform other operation
}


除此之外,refreshToken方法内的顺序取决于您在其中使用的方法。如果出于某种原因,您希望调用console.log('DEBUG::hihi3:');,请查看axios文档或它的含义。
无论如何,主要问题是您在if语句中使用了Promise,这总是会导致if语句跳过。

09-28 08:18