I have an array of items that I want to insert into the database one after another, something like this:
[{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
so basically, there would be a for..of loop iterating over this array and inserting them one after the another. I want to break out of the loop once an error happens, so for instance the error occurred after the 8th item:
[{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
^: error here
So I want to stop the execution at this phase, how to implement this? my code looks like this:
const [createOneUser, { isLoading, isError, isSuccess, error }] = useCreateOneUserMutation()
const bulkUsers = [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
const handleInsert = async()=>{
for(const obj of bulkUsers){
await createOneUser(obj) // <--- here
}
}
return <button onClick={handleInsert}>start bulk insert</button>
this looks different from how we usually do the stuff using useEffect
, but that's just a drafted plan to implement this. and I have no idea how to do this.
How to implement a similar logic in the redux toolkit query?