Consider example:
state = { x: false };
x = () => {
this.setState({ x: true });
};
y = async () => {
await this.x();
console.log(this.state.x);
^^^^ // logs true if used with await, logs false if await removed
};
y();
Since setState is async, the state update should happen after console.log
, so false
should be logged. And so it does.
HOWEVER!
If I use an async func and AWAIT the this.x()
, the true
value is being logged. How? Why?
Playground: https://stackblitz.com/edit/react-13fhfd?file=src%2FApp.js