New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"finish" event emitted before "readable" #204

Closed
sophister opened this Issue Aug 2, 2018 · 3 comments

Comments

Projects
None yet
2 participants
@sophister
Copy link

sophister commented Aug 2, 2018

hi , I have a csv file with two lines . and I'm using stream api like this:

const fileStream = fs.createReadStream(filePath);
const rows = [];
const parser = parse({
    skip_empty_lines: true,
});
parser.on('readable', () => {
    let record = null;
    while(record = parser.read()){
        rows.push(record);
    }
});
parser.on('error', (err) => {
    console.error('parser.error', err);
});
parser.on('finish', () => {
    console.log('finish rows: ', rows);
});

fileStream
    .pipe(iconv.decodeStream('gbk'))
    .pipe(parser);

when finish emitted, only 1 rows showed. using node v10.6.0 to debug, it turns out that after finish event emitted, one more readable event is evemitte, and at that time, I can get the second line.

Did I miss something?
thx in advance!

@wdavidw

This comment has been minimized.

Copy link
Collaborator

wdavidw commented Aug 2, 2018

The parser is both a writable and a readable stream. You write data and you read records. Following Node.js. stream documentation, the "finish" event is from the write API and is emitted when the input source has flushed its data. The "end" event is from the read API and is emitted when there is no more data to be consumed from the stream.

@wdavidw wdavidw closed this Aug 2, 2018

@sophister

This comment has been minimized.

Copy link

sophister commented Aug 2, 2018

@wdavidw
thx, using end works.
maybe Using the stream API section of the example doc needs to be updated, it is using finish event, and may bother others.

@wdavidw

This comment has been minimized.

Copy link
Collaborator

wdavidw commented Aug 2, 2018

Good catch, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment