【发布时间】:2022-01-04 07:32:34
【问题描述】:
我正在从 dispatch 函数发送数据(字符串)并且想要返回作为 POST 请求的结果返回的对象,但是由于 axios 是异步工作的,return 语句在 POST 请求的结果之前执行。请帮忙
dispatch({ type: "INCREASE_VOTES_QUESTION", payload: loginUser.id })
export default function reducer(state, action) {
if (action.type === "INCREASE_VOTES_QUESTION") {
let updatedSingleQuestion
axios({
method: "POST",
url: `http://localhost:5000/questions/${question_id}/votes-update`,
withCredentials: true,
data: { action: "increase", id: action.payload },
}).then((res) => {
updatedSingleQuestion = res.data
})
return updatedSingleQuestion
}
}
【问题讨论】:
-
你需要返回一个 axios 函数本身,它将作为一个承诺返回 stackoverflow.com/questions/48980380/…
标签: javascript reactjs axios