【发布时间】:2018-04-19 19:48:11
【问题描述】:
所以这里有一个矛盾:我想在 javascript/typescript 中创建一个异步阻塞队列(如果你可以在没有 typescript 的情况下实现它,那很好)。基本上我想实现类似 Java 的 BlockingQueue expect 之类的东西,而不是它实际上是阻塞的,它是异步的,我可以等待出队。
这是我要实现的接口:
interface AsyncBlockingQueue<T> {
enqueue(t: T): void;
dequeue(): Promise<T>;
}
我会这样使用它:
// 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();
}
有什么想法吗?
【问题讨论】:
标签: javascript typescript asynchronous promise async-await