Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
A non-blocking version of Atomics.wait! Instead of blocking the thread while waiting, it returns a Promise that resolves when the condition is met - perfect for multi-threaded JavaScript with Web Workers!
// Blocks the entire thread!
const result = Atomics.wait(
buffer, index, expectedValue
);
// Thread is frozen until notified
// Can't do anything else
// UI freezes (if on main thread)// Returns a Promise!
const { async, value } = Atomics.waitAsync(
buffer, index, expectedValue
);
// Thread continues running
if (async) {
value.then(() => {
console.log('Notified!');
});
}// Shared buffer between threads
const sharedBuffer = new SharedArrayBuffer(4);
const view = new Int32Array(sharedBuffer);
// Wait for value to change from 0
const { async, value } = Atomics.waitAsync(view, 0, 0);
if (async) {
// Returns a promise
value.then((result) => {
console.log('Result:', result); // 'ok' or 'timed-out'
});
} else {
// Resolved immediately
console.log('Immediate result:', value);
}// main.js (Main Thread)
const sharedBuffer = new SharedArrayBuffer(4);
const view = new Int32Array(sharedBuffer);
// Create worker
const worker = new Worker('worker.js');
worker.postMessage({ buffer: sharedBuffer });
// Wait for worker to signal completion
const { async, value } = Atomics.waitAsync(view, 0, 0);
if (async) {
console.log('Waiting for worker...');
value.then((result) => {
if (result === 'ok') {
console.log('Worker completed!');
console.log('Result:', view[0]);
}
});
}
// Main thread continues running!
console.log('Main thread not blocked');
// worker.js (Worker Thread)
self.onmessage = ({ data }) => {
const view = new Int32Array(data.buffer);
// Do heavy work
let result = 0;
for (let i = 0; i < 1000000; i++) {
result += Math.sqrt(i);
}
// Write result
Atomics.store(view, 0, result);
// Notify main thread
Atomics.notify(view, 0, 1);
};const sharedBuffer = new SharedArrayBuffer(4);
const view = new Int32Array(sharedBuffer);
// Wait with 5 second timeout
const { async, value } = Atomics.waitAsync(
view,
0, // index
0, // expected value
5000 // timeout in ms
);
if (async) {
value.then((result) => {
if (result === 'ok') {
console.log('Value changed!');
} else if (result === 'timed-out') {
console.log('Timeout reached');
}
});
}const result = Atomics.waitAsync(view, index, value);
// Result object has two properties:
// 1. async: boolean
// - true: Returns a Promise (async wait)
// - false: Resolved immediately (sync)
// 2. value: Promise<string> | string
// If async = true:
// - Promise that resolves to:
// - 'ok': Notified by Atomics.notify()
// - 'timed-out': Timeout reached
//
// If async = false:
// - 'not-equal': Current value doesn't match expected
// - 'timed-out': Timed out immediately
// Example handling
const { async, value } = Atomics.waitAsync(view, 0, 0);
if (async) {
// Async case - value is a Promise
await value; // 'ok' or 'timed-out'
} else {
// Sync case - value is a string
console.log(value); // 'not-equal' or 'timed-out'
}Thread continues executing - no freeze!
Returns Promise for async/await compatibility
Only works with SharedArrayBuffer for multi-threading
For complex multi-threaded applications with Workers
Doesn't freeze the thread while waiting
Returns Promise for async operations
For Web Workers and SharedArrayBuffer
Latest atomic operation enhancement