To write data from Immutable.js collections to an output stream, we set up a side-effect that iterates over the collection, writing each value to the stream. In simple cases, this is easy to do. In the more complex cases, you have to pay attention to concurrent and lazy evaluation.
Let's start by iterating over a collection and writing each value as a line to an output file:
const myList = Range() .map(v => `Value${v}`) .take(20) .toList(); const output = fs.createWriteStream( './output/05-writing-collection-data' ); output.on('close', () => { console.log('done'); }); myList.forEach((v) => { output.write((v + os.EOL)); }); output.end();
The values that we're trying to write to a file are simple strings from myList
. The output stream is ready for writing as soon as it's created with fs.createWriteStream()
. In the side-effect we've created with forEach()
, we're writing each value from the collection to...