Thursday, November 20, 2025
HomeLanguagesJavascriptJavaScript Atomics wait() Method

JavaScript Atomics wait() Method

The Atomics.wait() in JavaScript is an in-built method that is used to verify whether a given position in an Int32Array still contains a given value and if so sleeps, awaiting a wakeup or a timeout. The Atomics.wait() operation returns a string that is either “ok”, “not-equal”, or “timed-out”. The integer typedarray, index, and value are passed as an argument to the function and timeout is also an argument but it is optional.

Syntax: 

Atomics.wait(typedArray, index, value, timeout)

Parameters: This method accepts four parameters as mentioned above and described below: 

  • typedarray: This parameter specifies a shared integer typed array Int16Array.
  • index: This parameter specifies the position in the array, typedArray to wait on.
  • value: This parameter specifies the expected value to test.
  • timeout: This parameter is an optional parameter. It is time to wait in milliseconds.

Return value: The Atomics.wait() method returns the String which is either “ok”, “not-equal”, or “timed-out”.

Examples:

Input: arr[0] = 5
        Atomics.wait(arr, 0, 0, 1)
Output: not-equal

Input: arr[0] = 4
        Atomics.wait(arr, 1, 0, 1)
Output: time-out

The below programs illustrate Atomics.wait() method in JavaScript:

Example 1: This program illustrates the basic use of the Atomics.wait() method in JavaScript.

javascript




let buf = new SharedArrayBuffer(1024);
let arr = new Int32Array(buf);
  
arr[0] = 5;
console.log(Atomics.load(arr, 0));
console.log(Atomics.and(arr, 0, 9));
console.log(Atomics.wait(arr, 0, 0, 1));
console.log(Atomics.load(arr, 0));


Output: 

5
5
not-equal
1

Example 2: This program illustrates the basic use of Atomics.wait() method in JavaScript.

javascript




let buf = new SharedArrayBuffer(1024);
let arr = new Int32Array(buf);
  
arr[0] = 5;
console.log(Atomics.load(arr, 0));
console.log(Atomics.and(arr, 0, 9));
console.log(Atomics.wait(arr, 1, 0, 1));
console.log(Atomics.load(arr, 0));


Output: 

5
5
time-out
1

Exceptions: 

  • If the typedArray is not a shared Int32Array then the Atomics.wait() operation throws a TypeError.
  • If the index is used as an argument to the Atomics.wait() operation is out of the bound in the typedArray then the Atomics.store( ) operation throws a RangeError

We have a complete list of Javascript Atomics methods, to check those please go through the Javascript Atomics Complete Reference article.

Supported Browser:

  • Google Chrome
  • Microsoft Edge
  • Firefox
RELATED ARTICLES

Most Popular

Dominic
32404 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6776 POSTS0 COMMENTS
Nicole Veronica
11925 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11994 POSTS0 COMMENTS
Shaida Kate Naidoo
6905 POSTS0 COMMENTS
Ted Musemwa
7160 POSTS0 COMMENTS
Thapelo Manthata
6861 POSTS0 COMMENTS
Umr Jansen
6846 POSTS0 COMMENTS