3 ms·
Heh heh. I dont understand what the fuss is about, AsyncQueue is kinda cool in JS. I use it, from time to time, to implement async generators that can be iterat
by tekkk 1y ago
Heh heh. I dont understand what the fuss is about, AsyncQueue is kinda cool in JS. I use it, from time to time, to implement async generators that can be iterated over with for await.
Although my implementation doesnt have any sequencing as never had need for it but, more importantly, it has retrying and timeouts. Well retrying I might have implemented on level higher.
Maybe I'm just one of the rare few who actually would have enjoyed this type of question as a chance to brag about my version. Kinda neat as I've never been interested in programming challenges to, for once, know exactly the solution.
- tekkk 1y agoWhile I'm at it, here's my version, if people want to see and give feedback: export interface AsyncQueueOptions { timeoutSeconds?: number } export class AsyncQueue<T> { private readonly queue: Promise<T | undefined>[] = [] readonly timeoutSeconds: number private timeout: ReturnType<typeof setTimeout> | undefined private reject = () => {} private resolve = (value: T | PromiseLike<T>) => {} /** * @param timeoutSeconds @default 25 */ constructor({ timeoutSeconds = 25 }: AsyncQueueOptions = {}) { this.timeoutSeconds = timeoutSeconds if (timeoutSeconds > 100) { console.warn(`You are initializing AsyncQueue with over 100s timeout: ${timeoutSeconds}`) } this.queue.push( new Promise<T | undefined>((resolve, reject) => { this.resolve = resolve this.reject = reject this.timeout = setTimeout(() => resolve(undefined), timeoutSeconds * 1000) }) ) } next(): Promise<T | undefined> | undefined { return this.queue.shift() } push(msg: T) { this.resolve(msg) this.queue.push( new Promise<T | undefined>((resolve, reject) => { this.resolve = resolve this.reject = reject clearTimeout(this.timeout) this.timeout = setTimeout(() => resolve(undefined), this.timeoutSeconds * 1000) }) ) } close(msg?: T) { if (msg) { this.resolve(msg) } clearTimeout(this.timeout) } }