In studying streams, the following example was provided:
const data = ['some', 'data', 'to', 'read']
const { Readable } = require('stream')
const readable = Readable.from(data)
readable.on('data', (data) => { console.log('got data:', data) })
readable.on('end', () => { console.log('finished reading') })
This of course outputs got data: some
followed by got data: data
and so on and so forth for each array element.
I understand why a tutorial would use a simple example; however, I am struggling to imagine real world use cases for this snippet.
Speaking for the basic utility of the statement, it seems like no more than an obfuscation for any other way to loop over an array:
// eg.
const data = ['some', 'data', 'to', 'read']
data.map(data => console.log('got data:', data));
I see the benefit of the second being brevity and the con for the prior being complexity. However, the second snippet lacks flexibility for binding stream events. What other information is useful to help understand when and in what cases to implement a read stream in this way?
Please share a code snippet that makes use of the readable stream (from an array or otherwise) in a way that demonstrates the power of the flexibility. Also share any notes that explain better why a readable stream would be more useful in this or other cases.
#some more example