Hacker News new | ask | show | jobs
by jerf 5246 days ago
Ignoring WebWorkers, the underlying Javascript engine is single-threaded, so the problem you describe can't exist. Once you start down a code path, it will continue until it finishes and yields execution.

Further, "hold" is almost certainly a magical statement that actually compiles into a pattern of calls to .setTimeout and various handlers, and has no literal existence, so there probably isn't any point at which the hold is "executed".

You can't actually turn a single-thread runtime into a threaded runtime at the user level. You can apply a series of increasingly sophisticated hacks that may make program like it's multithreaded, but you can't escape the fact you have only one program counter. (This isn't a criticism of the library. It looks quite useful. It just doesn't magically make browser Javascript truly multithreaded.)

This approach is actually quite useful, and is part of what makes the event-based programming style practical. For all the stuff that's going on, you do always have the guarantee that any given event handler will fully execute, and no other hunk of code will be able to observe the half-executed state of any given handler. Without that property you'd basically just be doing conventional multithreading with a really inconvenient code structure.