3 ms·
Atomics.wait() cannot be called on the main thread (i.e. when your global is a Window object).
by domenicd 5y ago
Atomics.wait() cannot be called on the main thread (i.e. when your global is a Window object).
- fabiospampinato 5y agoIt 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?
- azakai 5y agoYes, 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!
- no_way 5y agoThere is Atomics.waitAsync which can be used on the main thread and just returns a promise. It is shipped at least in chrome.