Atomics.waitAsync()
Atomics.waitAsync()
静态方法在共享内存位置异步等待并返回 Promise
。
¥The Atomics.waitAsync()
static method waits asynchronously on a shared memory location and returns a Promise
.
与 Atomics.wait()
不同,waitAsync
是非阻塞的并且可在主线程上使用。
¥Unlike Atomics.wait()
, waitAsync
is non-blocking and usable on the main thread.
注意:此操作仅适用于查看
SharedArrayBuffer
的Int32Array
或BigInt64Array
。¥Note: This operation only works with an
Int32Array
orBigInt64Array
that views aSharedArrayBuffer
.
语法
参数
¥Parameters
typedArray
-
查看
SharedArrayBuffer
的Int32Array
或BigInt64Array
。 index
-
在
typedArray
的位置等待。 value
-
要测试的期望值。
timeout
Optional-
等待时间以毫秒为单位。
NaN
(以及转换为NaN
的值,例如undefined
)变为Infinity
。负值变为0
。
返回值
例外情况
¥Exceptions
TypeError
-
如果
typedArray
不是查看SharedArrayBuffer
的Int32Array
或BigInt64Array
,则抛出该异常。 RangeError
-
如果
index
在typedArray
中出界,则抛出该球。
示例
使用 waitAsync()
¥Using waitAsync()
给定一个共享的 Int32Array
。
¥Given a shared Int32Array
.
const sab = new SharedArrayBuffer(1024);
const int32 = new Int32Array(sab);
读取线程正在睡眠并等待位置 0,该位置预计为 0。result.value
将是一个承诺。
¥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 {<pending>} }
在读取线程或另一个线程中,调用内存位置 0,并且可以使用 "ok"
解析 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 {<fulfilled>: 'ok'} }
如果它没有解析为 "ok"
,则共享内存位置中的值不是预期的(value
将是 "not-equal"
而不是承诺)或已达到超时(承诺将解析为 "time-out"
)。
¥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"
).
规范
Specification |
---|
ECMAScript Language Specification # sec-atomics.waitasync |
浏览器兼容性
BCD tables only load in the browser
也可以看看
¥See also