JavaScript Atomics wait() Method

Beginner
7 min read
Updated: Nov 2026
3 examples
Atomics

Definition

Atomics.wait(typedArray, index, value, timeout) puts the current thread to sleep, but only if the element at index still equals value. It wakes up when another thread calls Atomics.notify() on the same element, or when timeout milliseconds have passed. It then returns a string: "ok" (woken by notify), "not-equal" (the value was already different, so it did not sleep at all), or "timed-out". Because it blocks the thread, it has strict rules: the array must be an Int32Array or BigInt64Array on a SharedArrayBuffer, and browsers only allow it inside workers, never on the page’s main thread. Node.js allows it on the main thread too.

Remember
Atomics.wait(typedArray, index, value)            -> "ok" | "not-equal" | "timed-out"
Atomics.wait(typedArray, index, value, timeout)

sleeps only while typedArray[index] === value
"ok"         woken by Atomics.notify()
"not-equal"  the value was already different
"timed-out"  timeout (ms) passed first

shared Int32Array / BigInt64Array only
browser: workers only   Node.js: main thread too

Syntax

Call it with or without a timeout in milliseconds. Without one, it can sleep forever.

JavaScript
Atomics.wait(typedArray, index, value)
Atomics.wait(typedArray, index, value, timeout)
CallWhat it does
Atomics.wait(typedArray, index, value)Sleeps until Atomics.notify() wakes this thread. Returns at once with "not-equal" if the element is not value.
Atomics.wait(typedArray, index, value, timeout)The same, but gives up after timeout milliseconds and returns "timed-out". A timeout of 0 checks once and never sleeps.

Part of JavaScript since ES2017. Its non-blocking cousin is Atomics.waitAsync(), which returns a promise and may be used on the browser main thread.

Library declarations
JavaScript
Atomics.wait(typedArray, index, value, timeout)
// returns "ok", "not-equal", or "timed-out"

// typedArray: Int32Array or BigInt64Array on a SharedArrayBuffer
// value:      Number (Int32Array) or BigInt (BigInt64Array)
// timeout:    milliseconds, default Infinity

Parameters

ParameterDescription
typedArrayRequired. An Int32Array or BigInt64Array whose buffer is a SharedArrayBuffer. Any other array type, or a normal (unshared) buffer, throws a TypeError.
indexRequired. The element to watch. An invalid position throws a RangeError.
valueRequired. The value you expect. The thread sleeps only while the element equals it. Use a BigInt such as 0n with a BigInt64Array.
timeoutOptional. The longest time to sleep, in milliseconds. The default is Infinity (wait until notified). Negative values act like 0.

Return Value

Return ValueDescription
"ok"The thread slept and another thread woke it with Atomics.notify().
"not-equal"The element did not equal value, so the thread never went to sleep.
"timed-out"The thread slept for the whole timeout and nobody woke it.

JavaScript Examples

Example 1 — The Two Results You Get Without Help

One thread alone can produce two of the three results. This runs on the Node.js main thread; the Try It page runs the same lines inside a Worker.

JavaScript
const shared = new Int32Array(new SharedArrayBuffer(8));
Atomics.store(shared, 0, 1);

console.log(Atomics.wait(shared, 0, 0, 10));   // not-equal
console.log(Atomics.wait(shared, 1, 0, 10));   // timed-out
console.log(Atomics.wait(shared, 1, 0, 0));    // timed-out
Try It Yourself

Browsers do not allow Atomics.wait() on the page’s main thread, so the Try It page runs this code inside a Worker. It also needs SharedArrayBuffer: in a normal (not cross-origin isolated) tab it prints a short explanation and the output above.

How It Works

  1. The value already moved on. Slot 0 holds 1 but we expect 0, so wait() returns "not-equal" right away without sleeping.
  2. Nobody comes to wake us. Slot 1 really is 0, so the thread sleeps for 10 ms. No other thread calls notify(), so the answer is "timed-out".
  3. Zero means just check. With a timeout of 0, wait() does not sleep at all; a matching value still reports "timed-out".

Example 2 — Arrays That wait() Refuses

wait() is picky about its array. This example needs no SharedArrayBuffer, so it runs everywhere, even on a browser main thread.

JavaScript
function tryWait(label, array) {
  try {
    Atomics.wait(array, 0, 0, 10);
    return label + ": waited";
  } catch (e) {
    return label + ": " + e.name;
  }
}

console.log(tryWait("Int32Array on ArrayBuffer", new Int32Array(4)));
console.log(tryWait("Uint8Array", new Uint8Array(4)));
console.log(tryWait("Float64Array", new Float64Array(4)));
Try It Yourself

How It Works

  1. Unshared memory is refused. An Int32Array on a normal ArrayBuffer cannot be woken by another thread, so waiting on it is a TypeError.
  2. Only two element types qualify. A Uint8Array is shared-capable but not an Int32 or BigInt64 array, so it is refused too.
  3. Floats never work with Atomics. A Float64Array fails the most basic check: Atomics only handles integers.

Example 3 — A Worker Sleeps and Is Woken: "ok"

The third result needs two threads. A Node.js worker reports "ready" and goes to sleep; the main thread writes a message and keeps calling notify() until one sleeper has been woken.

JavaScript
const { Worker } = require("worker_threads");

const shared = new Int32Array(new SharedArrayBuffer(8));   // [signal, message]
const worker = new Worker(
  "const { parentPort, workerData } = require('worker_threads');" +
  "parentPort.postMessage('ready');" +
  "const result = Atomics.wait(workerData, 0, 0);" +
  "parentPort.postMessage(result + ', message = ' + Atomics.load(workerData, 1));",
  { eval: true, workerData: shared }
);

worker.on("message", function (text) {
  if (text === "ready") {
    Atomics.store(shared, 1, 42);
    let woken = 0;
    while (woken === 0) woken = Atomics.notify(shared, 0, 1);
    console.log("main: woke " + woken + " worker");
  } else {
    console.log("worker: " + text);
    worker.terminate();
  }
});
Try It Yourself

The code above uses Node.js worker_threads. The Try It page runs the same steps with a browser Worker made from a Blob URL. It needs SharedArrayBuffer, so in a normal (not cross-origin isolated) tab it prints a short explanation and the output above.

How It Works

  1. The worker sleeps on slot 0. It calls Atomics.wait(workerData, 0, 0) with no timeout. Slot 0 stays 0 the whole time, so only notify() can end the sleep.
  2. Retry notify until someone wakes. “ready” can arrive a moment before the worker is really asleep. notify() returns 0 until then, so the loop repeats until it returns 1.
  3. The sleeper reports "ok". The woken worker reads slot 1, which was written before the notify, and posts ok, message = 42.

Browser & Runtime Support

Atomics.wait() is part of ES2017. Browsers allow it only inside workers, and only with SharedArrayBuffer, which needs a cross-origin isolated page.

✓ ES2017 · workers only in browsers

Atomics.wait()

Every modern browser supports it inside Web Workers on cross-origin isolated pages. Node.js (checked on version 24) also allows it on the main thread.

Workers Blocking waits stay off the page thread
Google Chrome Workers only · needs cross-origin isolation
Partial support
Mozilla Firefox Workers only · needs cross-origin isolation
Partial support
Apple Safari Workers only · needs cross-origin isolation
Partial support
Microsoft Edge Workers only · needs cross-origin isolation
Partial support
Opera Workers only · needs cross-origin isolation
Partial support
Samsung Internet Workers only · needs cross-origin isolation
Partial support
Internet Explorer No Atomics object
Not supported
Node.js Main thread and worker threads · checked on Node 24
Supported
Deno Check the main-thread rules of your version
Check version
Bun Check the main-thread rules of your version
Check version
Atomics.wait() Worker threads only

Bottom line: To use it in a browser, serve the page with Cross-Origin-Opener-Policy: same-origin and Cross-Origin-Embedder-Policy: require-corp (or credentialless), then call wait() inside a Worker. On the main thread, use Atomics.waitAsync() instead.

Frequently Asked Questions

One of three strings: "ok" when another thread woke it with Atomics.notify(), "not-equal" when the element already held a different value, and "timed-out" when the timeout passed first.
You called it on a browser main thread. Browsers forbid blocking there because it would freeze the page. Move the call into a Web Worker or use Atomics.waitAsync().
No. Only an Int32Array or BigInt64Array on a SharedArrayBuffer can be waited on. Anything else throws a TypeError, because no other thread could ever wake you.
Infinity. Without a timeout the thread sleeps until it is notified, so always make sure some thread will call Atomics.notify(), or pass a timeout in milliseconds.
A thread can wake up with "ok" or "timed-out" before the condition you care about is true. The safe pattern is: while (Atomics.load(a, i) === 0) Atomics.wait(a, i, 0);

Next: Atomics.waitAsync()

Wait without blocking: get a promise that resolves when the value is notified.

waitAsync() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful