Skip to content

Instantly share code, notes, and snippets.

@daveteu
Created October 18, 2023 09:15
Show Gist options
  • Save daveteu/723f7a10d8d20ba6624d7da4b9322228 to your computer and use it in GitHub Desktop.
Save daveteu/723f7a10d8d20ba6624d7da4b9322228 to your computer and use it in GitHub Desktop.
Fetch - Reading Stream
import ndjsonStream from 'can-ndjson-stream';
const getStream = async () => {
try {
const response = await fetch(url);
const reader = ndjsonStream(response.body).getReader();
let reader;
while (!result || !result.done) {
const data = result.value;
processLine(data) // Do things with line.
}
} catch (err) {
handleError(err)
}
}
@daveteu
Copy link
Author

daveteu commented Oct 18, 2023

To handle ndjson without using external package, see https://gist.github.com/daveteu/8517ff3845b1fe877d3b311519b67e6f on handling of buffer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment