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 shared Int32Array
or BigInt64Array
.
index
The position in the typedArray
to wait on.
value
The expected value to test.
timeout
OptionalTime to wait in milliseconds.Infinity
, if no time is provided.
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.
Examples
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); // { 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(the value
would be "not-equal"
instead of a promise)or the timeout was reached(the promise will resolve to "time-out"
).
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!