我在上下文中具有用于“创建会议”的异步功能,如下所示:
export const createMeet = dispatch => async (
token,
userId,
massagePressed,
date,
hour
) => {
dispatch({ type: "loading", payload: true });
if (token) {
try {
const response = await indexApi.post(
`/meeting/new/${userId}`,
{
massageType: massagePressed,
date,
hour,
},
{
headers: {
Authorization: `Bearer ${token}`,
},
}
);
console.log("inside context after request - message:",response.data.message);
dispatch({ type: "add_message", payload: response.data.message });///////this row important
dispatch({ type: "loading", payload: false });
return true;
} catch (err) {
dispatch({ type: "loading", payload: false });
return false;
}
}
};
现在我想显示模式消息回调取决于外部组件的请求(成功或失败),如下所示:
const createMeetHandler = async () => {
const createMeetCb = await bookingContext.createMeet(
state.token,
state.userId,
massagePressed.title,
dayPressed.date,
hourPressed.title
);
console.log("message before the bacllback exec", message);
showModal({
id: "MessageModal",
modalProps: {
type: createMeetCb ? "success" : "error",
message: message,
date: dayPressed.date,
hour: hourPressed.title,
massageType: massagePressed.title,
},
});
};
问题::异步功能完成,但是上下文中的消息不是exec是的。 控制台日志:
inside context after request - message: the meet is determined
message before the bacllback exec ////////////here its still empty (like initial message)
modalprops object
Object {
"id": "MessageModal",
"modalProps": Object {
"date": "08/11/2020",
"hour": "14:00 עד 15:00",
"massageType": "רגיל",
"message": "",
"type": "success",
},
}
您会看到,“显示模态”函数正在等待异步函数完成,但问题是异步完成不足以放置调度按摩并返回相同的初始消息(在所有“创建会议处理程序”功能完成后->然后进入消息分派)
问题:如何等待所有功能和调度完成。并执行“显示模态”功能?