type MutexHandle = { unlock: () => void; }; export default class Mutex { private chain: Promise = Promise.resolve(); readonly lock = () => new Promise((resolve) => { let unlock: () => void; const oldChain = this.chain; this.chain = new Promise((resolve) => { unlock = resolve; }); oldChain.then(() => resolve({ unlock, }) ); }); }