Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Async Iterator to Observable
function toObservable(asyncIter) {
return new Observable(sink => {
let abort = false;
(async _=> {
try {
for async (let x of asyncIter) {
if (abort) return;
sink.next(x);
}
} catch (x) {
sink.throw(x);
}
sink.return();
})();
return _=> {
abort = true;
};
});
}
async function* g1() {
await 1;
yield 1;
await 1;
yield 2;
}
toObservable(g1()).forEach(x => console.log("!!" + x));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.