DEV Community

Discussion on: Avoiding race conditions and memory leaks in React useEffect

Collapse
 
gpaoloni profile image
Gianfranco Paoloni

Great post, didn't knew about the AbortController, just learned cool stuff, thanks!

Just one small correction, I think that in the cleanup function of the AbortController example, you want to call abortController.abort().

Collapse
 
havespacesuit profile image
Eric Sundquist

Additionally, you can check the status of the abort signal before doing any state-changing assignments.

if (!abortController.signal.aborted) { /* set state */}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
saranshk profile image
saransh kataria

Though that can be done, it will never return true since whenever the signal will be aborted, the code would enter the catch block because of the exception thrown.

Collapse
 
paras594 profile image
Paras 🧙‍♂️

yes, I was thinking the same.

Collapse
 
saranshk profile image
saransh kataria

Thanks, that was a typo and I have updated it.