async-library / react-async

🍾 Flexible promise-based React data loader

Home Page:https://docs.react-async.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Potential race condition with deferFn run functions.

rstire364 opened this issue · comments

Demonstration: https://stackblitz.com/edit/react-2rszhm?file=src/App.js

We make use of context a great deal in our applications. Our intent was to use the deferFn, provide the entire state, and call the provided run function as necessary. This is working flawlessly when the run is called by a user action like a button press. However when programmatically calling the run function, like in a componentDidMount or useEffect(() => {}, []) the counter is incremented twice, the network request is fired, but the state never updates.

My assumption is theres a bit of race condition related to the counter initialization and the handleResolve call.

Yes, it also works when using the promiseFn, but this call is conditional. I'm certain we can rearchitect the DOM structure to circumvent this issue, but thought I'd bring it up.

Thanks for the package, big fans.