I'm batching mutation requests and firing them off with Promise.all however the callback will sometimes trigger before the mutation has. I can only recreate the issue when deleting single items. I know there's some gotchas when doing async/await within loops but I'm not sure if that's what's causing my problems.
What I'm trying to accomplish:
Gather all the mutations that need to be run (add, delete, and update have different endpoints and must be sent individually).
Trigger all the mutations and wait until they all successfully complete.
Refetch the list with all the updates applied
useQuery('todos', () => getTodosList(), { onSuccess: ({ todos }) => { setOriginalList(todos); }, }); const saveUpdates = useCallback(async () => { const mutations = []; for (let i = 0; i < list.length; i++) { const entry = list[i]; if (entry.modified) { let mutation; const { modified, id } = entry; if (modified === MODIFICATION.Deleted) { mutation = await deleteMutation.mutateAsync(id); } else if (modified === MODIFICATION.Updated) { mutation = await updateMutation.mutateAsync(entry); } else if (modified === MODIFICATION.Added) { mutation = await createMutation.mutateAsync(entry); } mutations.push(mutation); } } await Promise.all(mutations).then(() => { queryClient.invalidateQueries('todos'); }); },[...all the dependencies]);