Let's learn these things together.
The stream learning curve has been (embarrassingly) difficult for me. But I find that the more I talk about how confused I am, the more I find people are confused, too. I've received some great help along the way, and I want to share what I've found while I continue to learn more.
When you get stuck, keep in mind:
- The answer is almost always in the modules. The hard part is finding them and knowing what to use when
- Your use case can probably be "re-thought of" and applied to an existing solution for a similar problem
- You can come here to ask for help, and I'll try my best to answer or get one
- How can I make a paginated API request?
- How can I destroy all of the streams in a pipeline if one gets an error?
- How can I just get all of the results when they're done coming in?
- How can I asynchronously assign a stream after one has already been created?
- How do I know when a stream is first written to or read from?
- What's the difference between "abort", "close", "end", "destroy", "finish", and "complete"?
- Have a question?
If you find any of this information to be inaccurate or incomplete, feel free to contribute a PR!
How can I make a paginated API request?
You need to pull down many results from a backend, but it limits the amount of responses you receive at a given time. You get some type of token to include with a follow up request to cursor through the results. How do you combine all of those results into one stream?
- See an example
How can I destroy all of the streams in a pipeline if one gets an error?
You have a bunch of streams piped together and one gets an error. The other streams and any listeners on them don't really know what happened and linger around without being properly destroyed.
How can I just get all of the results when they're done coming in?
You have a source readable stream but don't really want to do anything stream-y with it. Registering
.on('data') events is a lot of boilerplate to combine the results as they come in.
How can I asynchronously assign a stream after one has already been created?
You want to make an API request but have to fetch an access token first. If you just call
request(/*...*/), you will inevitably get a 401 error, so how do you get a stream, but only have it "start" after you fetch an access token?
How do I know when a stream is first written to or read from?
Since it's possible for you (or a user of your library) to create a stream, but then never connect it to anything, or not run it for an unknown amount of time, you might want to wait as long as possible to do something async.
- See an example
- Note: works well with the duplexify problem.
What's the difference between "abort", "close", "end", "destroy", "finish", and "complete"?
There's a holy-moly-lot of events that go on with streams. How do you know which to listen to for a given stream?
Sometimes, you won't even know what kind of stream you have. Libraries may give you combined/duplex streams, a Transform stream, or a plain old readable stream. You really have to stay close to their documentation and source code to see what you're dealing with.
|close||Not always emitted. Readable streams emit this when the underlying resource is closed. Ex: a socket or a file that was "opened" to be read from.|
|complete||This is a custom event from
|end||All readable streams emit "end" when all of the data is read from. This in turn calls the
|finish||All streams that are writable emit "finish" when all of the data has been written to their destination.|
|abort()||This is a method called on request streams (
|destroy([err])||Destroy can be called on most new, non-core streams as a less-patient version of calling
|end()||This is a method that can be called on all streams that are writable to gracefully end the stream. Any remaining data that hasn't been written yet will be allowed to be drained. For readable streams, doing
More Helpful Resources
- Mississippi - See more examples and descriptions of the most popular stream modules (many of the ones mentioned here).