1

I am building out a function. It is not working properly and I believe it is because certain pieces of the function are not waiting for async calls to finish to completion. How can I get my function to run cleanly in the order indicated below?

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);
  }
};
Jevon Cochran
  • 1,565
  • 2
  • 12
  • 23
  • You need to provide the `updateStatus` and `deleteJobStatus` functions. If they are asynchronous, they should be awaited - if you do not await, they are just executed and the code continues without waiting for the result. – nbokmans Jun 10 '21 at 20:57

0 Answers0