Using the Atomics.load(typedArray, index) method

The Atomics.load method returns the value inside a typed array at a particular index value. Here's how to use it:

const sab = new SharedArrayBuffer(1);const arr = new Uint8Array(sab);arr[0] = 5;console.log(Atomics.load(arr, 0));

The preceding code is just a thread-safe way to access arr[0].

This outputs:

5

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.