implemented a basic asynchronous stack so, that I can do.
await stack.push(item);
const item = await stack.pop();
basically I'm registering listeners when ever i'm trying to pop an item over an empty stack and when ever i'm pushing an item i'm checking if there are any one waiting over for an item i'm sending it through the listener.
class AsyncStack {
constructor() {
this.stack = [];
this.waiting_minions = [];
}
async push(item) {
if (this.waiting_minions.length > 0) {
const signal = this.waiting_minions.pop();
await signal(item);
} else {
this.stack.push(item);
}
}
length() {
return this.stack.length;
}
async pop() {
return new Promise((resolve) => {
if (this.stack.length > 0) {
return resolve(this.stack.pop());
}
this.waiting_minions.push(resolve);
});
}
}