Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Atomics.waitAsync()

Limited availability

TheAtomics.waitAsync() static method verifies that a shared memory location contains a given value, immediately returning an object with thevalue property containing the string"not-equal" if the memory location does not match the given value, or"timed-out" if the timeout was set to zero. Otherwise the method returns an object where thevalue property is aPromise that fulfills with either"ok" whenAtomics.notify() is called, or"timed-out" if the timeout expires.

Atomics.waitAsync() andAtomics.notify() are used together to enable thread synchronization based on a value in shared memory. A thread can proceed immediately if the synchronization value has changed, or it can wait for notification from another thread when it reaches the synchronization point.

This method only works with anInt32Array orBigInt64Array that views aSharedArrayBuffer. It is non-blocking and, unlikeAtomics.wait(), can be used on the main thread. Because it does not block the whole thread, you still need to be careful not to access the shared memory before the promise settles.

Syntax

js
Atomics.waitAsync(typedArray, index, value)Atomics.waitAsync(typedArray, index, value, timeout)

Parameters

typedArray

AnInt32Array orBigInt64Array that views aSharedArrayBuffer.

index

The position in thetypedArray to wait on.

value

The expected value to test.

timeoutOptional

Time to wait in milliseconds.NaN (and values that get converted toNaN, such asundefined) becomesInfinity. Negative values become0.

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.

Exceptions

TypeError

Thrown iftypedArray is not anInt32Array orBigInt64Array that views aSharedArrayBuffer.

RangeError

Thrown ifindex is out of bounds in thetypedArray.

Examples

Using waitAsync()

Given a sharedInt32Array.

js
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.

js
const result = Atomics.waitAsync(int32, 0, 0, 1000);// { async: true, value: Promise {<pending>} }

In the reading thread or in another thread, the memory location 0 is called and the promise can be resolved with"ok".

js
Atomics.notify(int32, 0);// { async: true, value: Promise {<fulfilled>: '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").

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-atomics.waitasync

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp