creddy/src/lib/queue.js
2022-08-15 20:37:23 -07:00

24 lines
525 B
JavaScript

export default function() {
return {
items: [],
resolvers: []
put(item) {
this.items.push(item);
if (resolvers.length > 0) {
resolvers.shift().resolve();
}
},
async get() {
if (this.items.length === 0) {
await new Promise((resolve, reject) => {
this.resolvers.push(resolve);
})
}
return this.items.shift();
},
}
}