I have a function that streams data in batches via a callback.
Each batch will await the callback function before fetching another batch and the entire function retu
You need a event bucket, here is an example:
function bucket() {
const stack = [],
iterate = bucket();
var next;
async function* bucket() {
while (true) {
yield new Promise((res) => {
if (stack.length > 0) {
return res(stack.shift());
}
next = res;
});
}
}
iterate.push = (itm) => {
if (next) {
next(itm);
next = false;
return;
}
stack.push(itm);
}
return iterate;
}
;
(async function() {
let evts = new bucket();
setInterval(() => {
evts.push(Date.now());
evts.push(Date.now() + '++');
}, 1000);
for await (let evt of evts) {
console.log(evt);
}
})();