navigator.locks.request("my_resource", async (lock) => {
// The lock has been acquired.
await do_something();
await do_something_else();
// Now the lock will be released.
});Web Locks API / notebook
Web Locks API
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
await do_something_without_lock();
// Request the lock.
await navigator.locks.request("my_resource", async (lock) => {
// The lock has been acquired.
await do_something_with_lock();
await do_something_else_with_lock();
// Now the lock will be released.
});
// The lock has been released.
await do_something_else_without_lock();// Capture promise control functions:
const { promise, resolve, reject } = Promise.withResolvers();
// Request the lock:
navigator.locks.request(
"my_resource",
// Lock is acquired.
(lock) => promise, // Now lock will be held until either resolve() or reject() is called.
);