The Atomics.waitAsync()
static method waits asynchronously on a shared memory location and returns a Promise
.
Unlike Atomics.wait()
, waitAsync
is non-blocking and usable on the main thread.
Syntax
Atomics.waitAsync(typedArray, index, value)
Atomics.waitAsync(typedArray, index, value, timeout)
Return value
An Object
with the following properties:
-
async
-
A boolean indicating whether the value
property is a Promise
or not.
-
value
-
If async
is false
, it will be a string which is either "not-equal"
or "timed-out"
(only when the timeout
parameter is 0
). If async
is true
, it will be a Promise
which is fulfilled with a string value, either "ok"
or "timed-out"
. The promise is never rejected.
Using waitAsync()
Given a shared Int32Array
.
const sab = new SharedArrayBuffer(1024);
const int32 = new Int32Array(sab);
A reading thread is sleeping and waiting on location 0 which is expected to be 0. The result.value
will be a promise.
const result = Atomics.waitAsync(int32, 0, 0, 1000);
In the reading thread or in another thread, the memory location 0 is called and the promise can be resolved with "ok"
.
Atomics.notify(int32, 0);
If it isn't resolving to "ok"
, the value in the shared memory location wasn't the expected (the value
would be "not-equal"
instead of a promise) or the timeout was reached (the promise will resolve to "time-out"
).
Specifications
Browser compatibility
|
Desktop |
Mobile |
Server |
|
Chrome |
Edge |
Firefox |
Opera |
Safari |
Chrome Android |
Firefox for Android |
Opera Android |
Safari on IOS |
Samsung Internet |
WebView Android |
Deno |
Node.js |
waitAsync |
87 |
87 |
No |
75 |
16.4 |
89 |
No |
63 |
16.4 |
15.0 |
89 |
1.4 |
16.0.0 |