所以这是一个矛盾的说法:我想在javascript/typescript中创建一个异步阻塞队列(如果你可以在没有打字稿的情况下实现它,那很好).基本上我想实现类似Java的BlockingQueue预期,而不是实际阻塞它,它将是异步的,我可以等待出列.
这是我想要实现的接口:
interface AsyncBlockingQueue<T> {
enqueue(t: T): void;
dequeue(): Promise<T>;
}
Run Code Online (Sandbox Code Playgroud)
我会像这样使用它:
// enqueue stuff somewhere else
async function useBlockingQueue() {
// as soon as something is enqueued, the promise will be resolved:
const value = await asyncBlockingQueue.dequeue();
// this will cause it to await for a second value
const secondValue = await asyncBlockingQueue.dequeue();
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?