I've been bitten several times by bugs caused by the fact that calling setValue of a temporary state does not immediately update the value. I.e. if you get the value immediately after setting it, it will be the old value.
Even if you consider the value is set asynchronously and get it with setTimeout(..., 0) the behaviour is the same. I also noticed that setValue returns an object with a then property. This made me think that I can just await it, but this changes nothing.
How to reproduce:
Create a new app with a temporary state called state with an initial value of 1.
What about when running a script directly from an event handler? As @vangelov mentioned, await does nothing.
And I cannot toggle "Keep variable references inside the query in sync with your app." because it doesn't exist for these lightweight scripts. Even an absurdly long timeout delay won't have an effect; the value seems fully frozen by the context:
As it stands, temporary state vars in Retool seem pretty fraught — why would I not want to have variables' new values available virtually immediately after setting them in the vast majority of contexts? And why should I have to dig for a mostly undocumented, opt-in (in the full JS Query) to keep future references in sync?
Keeping variable references in sync actually comes with performance losses so it's recommended to only use the option when necessary, that's part of why it's turned off by default as well. The setting could certainly use more documentation! A number of people have expressed that keeping variables in sync is the expected behavior for them and it makes sense for that to be the case.
At the moment, though, the fix I'm aware of for the original issue is using the "Keep variable references inside the query in sync with your app" option.
@dguzzo and @Alex_Zak, would you mind explaining your use cases a bit more? Does using a JS query event handler with the setting turned on work?
Wow. I want that 4 hours of my life back and some head bandages please! Not the side effect I was expecting, also tried awaits and timers. Yuck. Checkbox worked for me. Thanks everyone. You are obviously doing a lot in editing mode. Maybe check to see if a write/read pattern is detected and warn people? I can't imagine any scenario that this behaviour is desirable other than speed like was mentioned.