Skip to content

Instantly share code, notes, and snippets.

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