In a greenfield environment things certainly would look different, but unfortunately we are tied to past decisions till browser vendors decide to create an alternate universe (well, webassembly ...) and that has enough spread to rely on. But for that one would need agreement what that better future would be.
We had this in Python and still have it in Lua, C, Zig, and so on, but the world has mostly gone the other way.
I can confirm that with careful engineering, application code doesn't have to suffer from the color problem. Fundamentally the trick is to yield after creating a callback, and resume into that callback when the thread has more data to process. A `foo:bar():baz():bux()` chain can pause execution in any of those methods and the user doesn't have to know which (some knowledge of whether this will happen /at all/ is required, only because you need a coroutine since Lua can't yield the main thread).
This isn't built-in, need to start with something like BEAM for that, but it's a nice sweet spot.
function doSomething(doSomethingElse) {
internalState.setUp();
doSomethingElse();
internalState.tearDown();
}
Right now, you can safely assume this whole thing will execute to completion without anything else happening in the interim, so a lot of existing JS makes that assumption. If `doSomethingElse` can yield while other code executes the assumption becomes invalid.A few languages avoid the pitfalls of threads, like Rust where its ownership system protects you.
If you have a remote database then you likely have multiple processes at play. For safety in those cases you need to rely on transactional safety at the database level or if transactions are not available then you may want to use a tool like TLA+ to check your distributed algorithm.
yield from server.update_balances([payer, payee])In that case you’d be relying on the database rather than the programming language to manage shared state. (The article is more about how to safely manage shared state within a process.)
[await:0] https://journal.stuffwithstuff.com/2015/02/01/what-color-is-... (read it whenever you would like, it's good I Promise).
If you've only ever used Javascript, it's tempting to think this is some fact of nature, rather than bad language design.