我正在构建一个函数。它工作不正常,我相信这是因为函数的某些部分没有等待异步调用完成。如何让我的函数按照下面指示的顺序干净地运行?
const handleStatusDelete = (status: JobStatusInterface) => {
let index = unlockedJobStatuses.findIndex((obj) => obj.id === status.id);
// Step 1. This loop should run completely.
for (let i = index + 1; i < unlockedJobStatuses.length - 1; i++) {
dispatch(
updateStatus({
id: unlockedJobStatuses[i].id,
status: { ...unlockedJobStatuses[i], order: unlockedJobStatuses[i].order - 1 },
}),
);
}
// Step 2. This action should run and finish.
dispatch(deleteJobStatus({ jobStatusId: status.id }));
// Step 3. This component state update action should run.
if (setOrderChangeToggle) {
setOrderChangeToggle(!orderChangeToggle);
}
};