Retrieve data from a ReadableStream object?
Get data from a ReadableStream
by getReader()
and read()
syntax:
This code repeatedly uses read()
, collects data chunks, then decodes, and logs the entire text.
Utilize Fetch API with ReadableStream
Fetch from APIs with fetch()
, parse JSON data in style with response.json()
or get text data flexy with response.text()
. To flex your binary data muscles, use the ever-powerful response.arrayBuffer()
.
Psst... all calls to response.json()
, response.text()
, and response.arrayBuffer()
automatically concatenates and decodes the stream for you. Handy, right?
Manual ReadableStream parsing for the brave
Manual parsing of a ReadableStream is as thrilling as a blockbuster movie. Use getReader()
and a stream-reading loop to act out every chunk drama. TextDecoder
is your best buddy—use it to concatenate and decode the chunks.
Taming errors and the less-traveled path
Catch potential bugs in action using catch()
(always implement error handling, no exceptions!). Juggle with decoding method or custom parser production for supporting non-standard data types or encodings.
Node.js follows a different stream flow. Node provides a stream
and the all-new shiny stream/consumers
API to soak up streams in an environment-friendly fashion.
For scenario-specific data transformations, refer to the shiny TransformStream
class or pipeline-streams using libraries.
Was this article helpful?