Atomics.waitAsync() - JavaScript Atomics 对象
Atomics.waitAsync()
静态Atomics.waitAsync()
方法在共享内存位置上异步等待,并返回Promise
。
与Atomics.wait()
,waitAsync
是非阻塞的,可以在主线程上使用
注意:此操作仅适用于共享的Int32Array
或BigInt64Array
。
语法
Atomics.waitAsync(typedArray, index, value) Atomics.waitAsync(typedArray, index, value, timeout)
Parameters
typedArray
A sharedInt32Array
orBigInt64Array
.
index
The position in thetypedArray
to wait on.
value
The expected value to test.
timeout
OptionalTime to wait in milliseconds.Infinity
, if no time is provided.
Return value
AnObject
with the following properties:
async
A boolean indicating whether thevalue
property is aPromise
or not.
value
Ifasync
isfalse
, it will be a string which is either"not-equal"
or"timed-out"
(only when thetimeout
parameter is0
). Ifasync
istrue
, it will be aPromise
which is fulfilled with a string value, either"ok"
or"timed-out"
. The promise is never rejected.
Examples
Given a sharedInt32Array
.
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. Theresult.value
will be a promise.
const result = Atomics.waitAsync(int32, 0, 0, 1000); // { async: true, value: Promise {} }
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); // { async: true, value: Promise {: 'ok'} }
If it isn't resolving to"ok"
, the value in the shared memory location wasn't the expected(thevalue
would be"not-equal"
instead of a promise)or the timeout was reached(the promise will resolve to"time-out"
).