Skip to content

Instantly share code, notes, and snippets.

@daretodave
Created April 17, 2020 07:31
Show Gist options
  • Save daretodave/c8ebff482887ee66b03c6bfacd8ff28c to your computer and use it in GitHub Desktop.
Save daretodave/c8ebff482887ee66b03c6bfacd8ff28c to your computer and use it in GitHub Desktop.
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