Javascript 链路内部的等待功能
我每次执行apollo API调用时都会运行这部分代码。我的目标是在进行API调用之前检查用户是否经过身份验证,是否具有最新刷新的令牌。我的问题是,我无法等待注释掉的行,因为函数不是异步的Javascript 链路内部的等待功能,javascript,reactjs,apollo,Javascript,Reactjs,Apollo,我每次执行apollo API调用时都会运行这部分代码。我的目标是在进行API调用之前检查用户是否经过身份验证,是否具有最新刷新的令牌。我的问题是,我无法等待注释掉的行,因为函数不是异步的 const authenticationLink = new ApolloLink((operation, forward) => { // const currentSession = await Auth.currentSession(); // setAccessToken(curren
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设置为异步,我是否需要更改下面的其他代码以等待它,或者它是否可以保持不变?等待与异步一起使用。(
替换
const authenticationLink = new ApolloLink((operation, forward) => {
到
不幸的是,这导致了代码中的错误。网络错误:inner.subscribe不是一个函数请查看那些Github答案>
const authenticationLink = new ApolloLink(async (operation, forward) => {