How about this for a paradox: I'm looking to develop an asynchronous blocking queue in JavaScript/TypeScript (or any other language if Typescript is not feasible). Essentially, I want to create something similar to Java's BlockingQueue
, but instead of being truly blocking, it would function asynchronously and allow for awaiting dequeues.
Here's the outline of the interface I aim to build:
interface AsyncBlockingQueue<T> {
enqueue(t: T): void;
dequeue(): Promise<T>;
}
And here is how I envision using it:
// Enqueue items from another place
async function utilizeBlockingQueue() {
// Once there is an item enqueued, the promise will be fulfilled:
const value = await asyncBlockingQueue.dequeue();
// This will prompt waiting for a second value
const secondValue = await asyncBlockingQueue.dequeue();
}
Any suggestions?