与这个异步thunk混淆
export const loginSuccess = createAsyncThunk(
"auth/loginSuccess",
async (user: User) => {
const res = await api
.post(
"/auth/loginSuccess",
{ user },
{
withCredentials: true,
}
)
.then((res: any) => {
setAxiosToken(res.data.token);
saveToken(res.data.token);
return { ...res.data.data, token: res.data.token };
});
return res;
}
);
最后有2个返回语句,因此我对满足的还原器将获得哪个返回值感到困惑。该代码是由其他人撰写的,这就是为什么我想理解它。
export const loginSuccess = createAsyncThunk(
"auth/loginSuccess",
async (user: User) => {
const res = await api
.post(
"/auth/loginSuccess",
{ user },
{
withCredentials: true,
}
)
.then((res: any) => {
setAxiosToken(res.data.token);
saveToken(res.data.token);
return { ...res.data.data, token: res.data.token };
});
return res;
}
);
There are 2 return statements at the end so I am confused about which return value the fulfilled reducer will get. The code is written by someone else that's why I want to understand it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第二个返回语句是将从您的功能返回的语句。
第一个实际上是从
然后从
函数返回的承诺axios
返回的函数。通过在thunk函数中使用
res
变量的相同名称以及传递给然后
函数的响应变量,这使它有些混乱。但是,您将收到的是代码行中生成的对象:
其中
res.data.data
被扩散到新对象中,res.data.token
is分配给该对象的令牌属性。The second return statement is the one which will return from your function.
The first is actually returning from the
then
function of the promise thataxios
returns.This is made a little bit confusing by using the same name for the
res
variable in the thunk function, and for the response variable that is passed to thethen
function.But what you will receive back is the object generated in this line of code:
Where
res.data.data
is spread into a new object, andres.data.token
is assigned to the token property of that object.