Hacker News new | past | comments | ask | show | jobs | submit login

Atomics.wait() cannot be called on the main thread (i.e. when your global is a Window object).



It works fine both in Node and Deno. I haven't tested this out on browsers though, is that supposed to throw even if SharedArrayBuffer is enabled?


Yes, browsers will not allow you to block on the main thread. Atomics.waitAsync is supposed to be used instead.

This is a fairly difficult aspect of multithreading on the web, and it makes things more complicated than other platforms (like Node and Deno, as you mentioned). For example in emscripten's pthreads support layer there is code dedicated to do a sort of careful busy-wait when we have no other option, and all that is only for the case of the main thread.

But your point is still very relevant, just not on the main thread: if you can run your application in a worker, then you can block on Promises using another worker that does the async operation while the first worker is synchronous. And that's really useful!


There is Atomics.waitAsync which can be used on the main thread and just returns a promise. It is shipped at least in chrome.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: