Async Reader-Writer Lock
Coordinate concurrent async readers and exclusive writers with a fair, promise-based reader-writer lock.
Code
export class AsyncReaderWriterLock {
private readers = 0;
private writer = false;
private queue: Array<{ write: boolean; resolve: () => void }> = [];
async acquireRead(): Promise<() => void> {
await this.request(false);
this.readers += 1;
return () => this.release(false);
}
async acquireWrite(): Promise<() => void> {
await this.request(true);
this.writer = true;
return () => this.release(true);
}
private request(write: boolean): Promise<void> {
if (this.queue.length === 0 && !this.writer && (write || this.readers >= 0)) {
if (write ? this.readers === 0 : !this.writer) return Promise.resolve();
}
return new Promise((resolve) => this.queue.push({ write, resolve }));
}
private release(write: boolean) {
if (write) this.writer = false;
else this.readers -= 1;
this.drain();
}
private drain() {
while (this.queue.length > 0) {
const next = this.queue[0] as { write: boolean; resolve: () => void };
if (next.write) {
if (this.writer || this.readers > 0) return;
(this.queue.shift() as { resolve: () => void }).resolve();
return;
}
(this.queue.shift() as { resolve: () => void }).resolve();
}
}
}How the snippet works
The working surface is the class AsyncReaderWriterLock — everything else is wiring.
while (this.queue.length > 0) is the loop that carries the per-item work. Asynchronous boundaries use await, so failures surface as rejected promises at the call site instead of being swallowed.
It builds and returns a fresh value rather than reusing its inputs.
Points of care
- The guard
write ? this.readers === 0 : !this.writershort-circuits the flow with an earlyreturnwhen its condition holds.
Applying it
AsyncReaderWriterLock is what another module would consume; nothing else here is load-bearing for callers.
Statements that matter
return new Promise((resolve) => this.queue.push({ write, resolve }));
if (write ? this.readers === 0 : !this.writer) return Promise.resolve();
async acquireWrite(): Promise<() => void> {
await this.request(true);
this.writer = true;
return () => this.release(true);
}
Interactive Tools for This Topic
FAQ
- Does `AsyncReaderWriterLock` modify its input?
- The input survives unchanged; what you get back is a newly constructed value.
- Is a package install part of the setup?
- No. Installation is unnecessary because only built-in APIs are used.
Report a problem with this exampleOpens a pre-filled issue on GitHub.