Created
October 18, 2023 09:15
-
-
Save daveteu/723f7a10d8d20ba6624d7da4b9322228 to your computer and use it in GitHub Desktop.
Fetch - Reading Stream
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 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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To handle ndjson without using external package, see https://gist.github.com/daveteu/8517ff3845b1fe877d3b311519b67e6f on handling of buffer