export class Mutex {
  private current: Promise<void> = Promise.resolve();

  async run<T>(fn: () => Promise<T>): Promise<T> {
    const next = this.current.then(fn, fn);
    this.current = next.then(
      () => undefined,
      () => undefined
    );
    return next;
  }
}
