Tomas Zaicevas
1 min readMar 10, 2022

--

That's a very, very interesting question. I'll share with you the way I think about it.

Whenever you call setState, it's up for React to decide when to execute it. It might be that React calls setTimeout and causes the callback to get into task/microtask queues. But it might also be that React holds the state updates in-memory (e.g. in an array) and synchronously executes them when the right moment occurs. I haven't checked, but I'd assume that's how rendering with RTL and act() works - state update is held in-memory until act() is called either in the test or in the RTL, and then the state update is synchronously flushed.

The term "asynchronous", in React state update case, holds a slightly different meaning than in await fetch case. As I see it, it means "it is up for React to decide when it is going to happen". So I completely get why you got confused.

--

--

Responses (1)