Skip to content

Instantly share code, notes, and snippets.

@jooyunghan
Last active December 22, 2017 13:51
Show Gist options
  • Save jooyunghan/d9aa1de22540d042e8bdbc36d84d67e9 to your computer and use it in GitHub Desktop.
Save jooyunghan/d9aa1de22540d042e8bdbc36d84d67e9 to your computer and use it in GitHub Desktop.
AsyncIterator for Observable Using Ag() coroutine
function createAsyncIterator() {
const self = this;
return ag(function*() {
const resume = [];
const values = [];
const subscription = self.materialize().subscribe(value => {
values.push(value);
resume.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 == 'E') throw v.error;
else return;
}
yield new Promise(r => resume.push(r));
}
} finally {
subscription.unsubscribe();
}
});
}
@jooyunghan
Copy link
Author

jooyunghan commented Dec 22, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment