问题描述
我每次执行 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) => {