Skip to content

Instantly share code, notes, and snippets.

@jooyunghan
Created December 21, 2017 12:45
Show Gist options
  • Save jooyunghan/3aa58ca5f8808c1211c62b65226a01eb to your computer and use it in GitHub Desktop.
Save jooyunghan/3aa58ca5f8808c1211c62b65226a01eb to your computer and use it in GitHub Desktop.
async function* createAsyncIterator() {
const promise = [];
const values = [];
const subscription = this.materialize().subscribe(value => {
values.push(value);
promise.splice(0).forEach(r => r());
});
try {
while (true) {
while (values.length > 0) {
const v = values.shift();
if (v.kind == 'N') yield v.value;
else if (v.kind == 'C') return;
else if (v.kind == 'E') throw v.error;
}
await new Promise(r => promise.push(r));
}
} finally {
subscription.unsubscribe();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment