/** * Ensures that provided promises are called in the order they were received, one at a time. */ export class PromiseQueue { private promise: Promise = Promise.resolve(); private running = true; add(exec: () => Promise): Promise { if (!this.running) { return Promise.reject(new Error('PromiseQueue is closed')); } const typedPromise = this.promise.then(exec); this.promise = typedPromise.catch(() => undefined); return typedPromise; } async shutdown(): Promise { this.running = false; return await this.promise.then(); } }