微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ApolloLink 中的等待功能

如何解决ApolloLink 中的等待功能

我每次执行 apollo API 调用时都会运行这部分代码。我的目标是在进行 API 调用之前检查用户是否已通过身份验证并拥有最新的刷新令牌。我的问题是我无法等待注释掉的行,因为该函数不是异步的

const authenticationLink = new ApolloLink((operation,forward) => {

  // const currentSession = await Auth.currentSession();
  // setAccesstoken(currentSession.signInUserSession.idToken.jwtToken)

  if (typeof token === "string") {
    operation.setContext(() => ({
      headers: {
        Authorization: token
      }
    }));
  }

  return forward(operation);
});


const standardLink = ApolloLink.from([
  authenticationLink,httpLink
]);

如果我使 authenticationLink 异步,我需要更改下面的其他代码以等待它还是保持不变?

解决方法

await 与 async 一起使用。 (More details here)

替换

const authenticationLink = new ApolloLink((operation,forward) => {

const authenticationLink = new ApolloLink(async (operation,forward) => {

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。