Skip to content

Instantly share code, notes, and snippets.

@mary-ext
Last active May 11, 2024 14:09
Show Gist options
  • Save mary-ext/7ffb275fd77c8e060c9eba67aa350b8d to your computer and use it in GitHub Desktop.
Save mary-ext/7ffb275fd77c8e060c9eba67aa350b8d to your computer and use it in GitHub Desktop.
Queued promise tasks
export class PromiseQueue {
#queue: { deferred: PromiseWithResolvers<any>; fn: () => any }[] = [];
#max: number;
#current = 0;
constructor({ max = 4 }: { max?: number } = {}) {
this.#max = max;
}
add<T>(fn: () => Promise<T>): Promise<T> {
const deferred = Promise.withResolvers<T>();
this.#queue.push({ deferred, fn });
this.#run();
return deferred.promise;
}
#run() {
if (this.#queue.length > 0 && this.#current <= this.#max) {
const { deferred, fn } = this.#queue.shift()!;
this.#current++;
const promise = new Promise((r) => r(fn()));
const done = () => {
this.#current--;
this.#run();
};
promise.then(
(res) => {
done();
deferred.resolve(res);
},
(err) => {
done();
deferred.reject(err);
},
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment