Created
April 17, 2020 07:31
-
-
Save daretodave/c8ebff482887ee66b03c6bfacd8ff28c 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'); | |
async function* request(completed) { | |
const raceList = await Promise.race([ | |
once(inbound, 'data'), | |
completed, | |
]); | |
const line = raceList[0]; | |
if (!line) { | |
return { done: true }; | |
} | |
yield line.toString(); | |
yield* request(completed); | |
} | |
return request( | |
once(inbound, 'end'), | |
); | |
} | |
stream("https://www.google.com/").then(o => o.next()).then(console.log).catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment