You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, there is no ways to synchronously wait or sleep in the main thread without an active wait, which is CPU intensive.
Well we can call a long synchronous function (with potentially a timeout, e.g. synchronous AJAX calls), but this is quite dirty.
Good news, Atomics.pause() will allow to sleep for a short amount of time (only for 10 to 100 nano-seconds, still >10,000x less CPU intensive). It won't free the CPU, but will avoid doing an intensive wait.
Atomics.pause() is now at the Stage 2.7 of TC39.
This means it might "soon" be included into browsers.
Though, it won't be possible to wait for a promise to finish as it will pause the main thread (so the micro-task queue won't run). Indeed, we can't get values from a promise without await or then.
One work-around could be to perform the asynchronous operation in a webworker, and to store a flag/result in a shared array ?
(There is also a shared struct that might comes to JS standard in order to better communicate with webworkers).
I'll try to keep you updated on this new JS feature.
The text was updated successfully, but these errors were encountered:
Currently, there is no ways to synchronously wait or sleep in the main thread without an active wait, which is CPU intensive.
Well we can call a long synchronous function (with potentially a timeout, e.g. synchronous AJAX calls), but this is quite dirty.
Good news,
Atomics.pause()
will allow to sleep for a short amount of time (only for 10 to 100 nano-seconds, still >10,000x less CPU intensive). It won't free the CPU, but will avoid doing an intensive wait.Atomics.pause()
is now at the Stage 2.7 of TC39.This means it might "soon" be included into browsers.
Though, it won't be possible to wait for a promise to finish as it will pause the main thread (so the micro-task queue won't run). Indeed, we can't get values from a promise without
await
orthen
.One work-around could be to perform the asynchronous operation in a webworker, and to store a flag/result in a shared array ?
(There is also a
shared struct
that might comes to JS standard in order to better communicate with webworkers).I'll try to keep you updated on this new JS feature.
The text was updated successfully, but these errors were encountered: