ECMAScript question detail
Atomic waitSync
The Atomics.waitAsync() is a static method that waits asynchronously on a shared memory location and returns a Promise. It is non-blocking as compared to Atomics.wait() and can be used on the main thread. The syntax looks like below,
Atomics.waitAsync(typedArray, ind, val, timeOut);
If the promise is not fulfilled then it will lead to a 'time-out' status otherwise the status will always be 'ok' once the promise has been fulfilled.
Let's take a shared Int32Array. Here it waits asynchronously for position 0 and expects a result 0 waiting for 500ms.
const arrayBuffer = new SharedArrayBuffer(1024);
const arr = new Int32Array(arrayBuffer);
Atomics.waitAsync(arr, 0 , 0 , 500); // { async: true, value: Promise {<pending>}}
Atomics.notify(arr, 0); // { async: true, value: Promise {<fulfilled>: 'ok'} }
After that, the notify method awakes the waiting agent(i.e, array) that are sleeping in waiting queue and the promise is fulfilled.
Remember that, SharedArrayBuffer have been disabled on most browsers unless you specify Cross-Origin-Opener-Policy and Cross-Origin-Embedder-Policy headers. For example,
Cross-Origin-Opener-Policy: same-origin Cross-Origin-Embedder-Policy: require-corp
Note: There will be a TypeError if typedArray is not an Int32Array or BigInt64Array that views a SharedArrayBuffer.