Created
May 28, 2015 13:42
-
-
Save zenparsing/0557442eee0b89f361ac to your computer and use it in GitHub Desktop.
Async Iterator to Observable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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