Skip to content

Instantly share code, notes, and snippets.

@DanShappir
Created May 14, 2020 14:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanShappir/3e6af0a875ceb929d05c5b1f6f87385e to your computer and use it in GitHub Desktop.
Save DanShappir/3e6af0a875ceb929d05c5b1f6f87385e to your computer and use it in GitHub Desktop.
function fromCallback(emitter) {
let values;
let resolve;
const init = r => [values, resolve] = [[], r];
let valuesAvailable = new Promise(init);
emitter(value => {
values.push(value);
resolve(values);
});
return {
[Symbol.asyncIterator]: async function*() {
for (;;) {
const vs = await valuesAvailable;
valuesAvailable = new Promise(init);
yield* vs;
}
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment