I’ve been playing around with streams and I cannot explain the following behaviour:
import * as fs from 'fs/promises';
import * as csv from 'csv';
const MERCHANT_NAME = 'lm';
const run = async () => {
const fileContent = await fs.readFile('./input', { encoding: 'utf-8'});
const parser = csv.parse(fileContent);
const output = parser.map(
([id, pm, psp]: [string, string, string]) => ({
merchantName: MERCHANT_NAME,
providerName: psp,
paymentMethod: pm,
userId: +id,
data: null,
}),
);
const userIdsToken = await output
.map((record) => record.userId)
.reduce((acc, cv) => (acc === '' ? acc + cv : acc + ',' + cv), '');
console.log('output.destroyed', output.destroyed); // true
output.forEach(r => console.log(r)); // ain't going to print out nothing...
const output2 = parser.map(
([id, pm, psp]: [string, string, string]) => ({
merchantName: MERCHANT_NAME,
providerName: psp,
paymentMethod: pm,
userId: +id,
data: null,
}),
);
console.log('output2.destroyed', output2.destroyed); // false
output2.forEach(r => console.log(r)); // ain't going to print out nothing...
}
run();
There’s a stream called output of type internal.Readable
which is destroyed when userIdsToken
is computed. Then I try creating another stream output2
which is not destroyed but still output2.forEach(r => console.log(r))
does not print anything.
What is happening and how to fix it?