export class Mutex { private pending: Promise = Promise.resolve(); async run(operation: () => Promise): Promise { const previous = this.pending; const { promise, resolve: release } = Promise.withResolvers(); this.pending = promise; await previous; try { return await operation(); } finally { release(); } } }