Skip to content

Instantly share code, notes, and snippets.

@ancms2600
Created April 22, 2020 18:05
Show Gist options
  • Save ancms2600/09df67681fcdcfb4175a0c6c145a07f6 to your computer and use it in GitHub Desktop.
Save ancms2600/09df67681fcdcfb4175a0c6c145a07f6 to your computer and use it in GitHub Desktop.
Iterator Loop
Utils.iteratorLoop = () => {
const buf = [];
let next, done = false;
const push = e => {
buf.push(e);
if (null != next) next();
};
const loop = async function* () {
while (true) {
yield* buf.splice(0);
if (done) break;
await new Promise(ok=>{next=ok;});
}
};
return { iter: loop(), push, done: () => { done = true; } };
};
// const { iter, push, done } = iteratorLoop();
// state.c.on('connect', (...args) => push({ name: 'connect', args }));
// return iter;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment