Created
April 17, 2020 07:55
-
-
Save daretodave/c85b9a740daca980d96b2128d9919338 to your computer and use it in GitHub Desktop.
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
import {get} from "https" | |
import {once} from "events"; | |
export async function stream(url) { | |
const [inbound] = await once(get(url), 'response'); | |
const completed = once(inbound, 'end'); | |
async function* request() { | |
const [line] = await Promise.race([ | |
once(inbound, 'data'), | |
completed, | |
]); | |
if (!line) { | |
return {done: true} | |
} | |
yield line; | |
yield* request(); | |
} | |
return request(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment