Find yourself with a callback you'd like to make its passed values iterable--ie useful in a for await
loop?
function Streamer(key) {
const _ = Object.create(null);
_.key = key;
_.resolver = null;
_.dataQueue = [];
const push = function (newData) {
if (this.resolver) {
this.resolver({ value: newData, done: false });
this.resovler = null;
} else {
this.dataQueue.push(newData);
}
}.bind(_);
const pull = function () {
const self = this;
return new Promise((r) => {
if (self.dataQueue.length) {
r({ value: self.dataQueue.shift(), done: false });
} else {
self.resolver = r;
}
});
}.bind(_);
return {
push,
pull,
[Symbol.asyncIterator]() {
return {
next: this.pull,
};
},
};
}
Now can be used to create Readable
wrapped data sources so that the data ca... must flow.
Best way to handle error/rejection case? Note while there are many settled resolutions, should only need to be one rejection else restart.