Several tabs of the warung’s app sell from one stock book: what is the final stock with no lock, with navigator.locks.request, and with ifAvailable and no…
Why it matters
Tabs of the same app run at the same time and fight over the same resource: a refresh token, offline sync, an upload queue. Without coordination you get a *lost update*: one write overwrites another. Web Locks gives you a cross-tab mutex from the browser itself, so you need no race-prone flag in localStorage.
The key idea
request() holds the lock while its callback runs, then lets go by itself
Common mistakes
Looking for an unlock() to release a lock by hand There is none. The lock is let go by itself once the callback’s promise settles. So do not await long, unneeded work inside the callback: every other tab is held back meanwhile.
Expecting the lock to hold back another computer, when it only covers one browser Web Locks only holds for one origin in one browser. It is not a distributed lock: a phone or another browser has a lock manager of its own. That needs coordination on the server.
Using ifAvailable: true, and the critical section runs anyway when the lock was not granted When the lock is not available the callback is still called, with null. You must if (!lock) return so the critical section never runs without the lock.
Try it in this playground
No lock: one sale is overwritten Bagas writes 9 over Mrs. Sari’s 9.
Waiting in the queue With request('stok'), the second tab waits for the lock.
The lock moves on with no unlock() The callback is done, and the lock goes straight to the one waiting.
ifAvailable without a check Bagas's callback gets null and sells anyway.