Skip to content

Instantly share code, notes, and snippets.

@hyousi
Last active January 29, 2024 03:48
Show Gist options
  • Save hyousi/983ae41016399332942744bb2a5044bb to your computer and use it in GitHub Desktop.
Save hyousi/983ae41016399332942744bb2a5044bb to your computer and use it in GitHub Desktop.
http stream

Source: https://developer.chrome.com/docs/capabilities/web-apis/fetch-streaming-requests?hl=zh-cn

const response = await fetch(url);
const reader = response.body.getReader();
// if you want to read the data as text
const reader = response.body.pipeThrough(new TextDecoderStream()).getReader();

while (true) {
  const { value, done } = await reader.read();
  if (done) break;
  console.log("Received", value); // the value is a Uint8Array
}

console.log("Response fully received");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment