Skip to content
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

promise() not listen for 'close' event #559

Open
tjpgt opened this issue Feb 11, 2022 · 3 comments
Open

promise() not listen for 'close' event #559

tjpgt opened this issue Feb 11, 2022 · 3 comments

Comments

@tjpgt
Copy link

tjpgt commented Feb 11, 2022

The promise() function listens for 'finish' and 'error', but dosen't listen for 'close'.

For example, I have a http server, and use json2csv to export a csv. I use toOutput to pipe to http response, then use promise() and wait for the promise. When I start a request and cancel the requset(use postman, for example), the http response emit a 'close' event, but the promise never settle.

Code like this:

const asyncParser = new AsyncParser(opts, transformOpts);
const parsePromise = asyncParser.toOutput(res).promise();          // res is the http response
......
read much data and push to asyncParser.input
......
asyncParser.input.push(null);
await parsePromise  // if 'close' event but no 'finish' or 'error' is emitted on res, it will never resolve or reject

Though I can write my own promise listen for 'close' event instead of using promise(). It would be better if it's added in the promise() function in json2csv.

@juanjoDiaz
Copy link
Collaborator

This is an interesting one...
Any idea of why is dispatching close but not finish?
Is it a bug?

I would say that it should either finish or error.

@tjpgt
Copy link
Author

tjpgt commented Feb 24, 2022

This is an interesting one... Any idea of why is dispatching close but not finish? Is it a bug?

I would say that it should either finish or error.

image

From nodejs api doc: https://nodejs.org/docs/latest-v14.x/api/http.html#http_class_http_serverresponse

So I think if the response is sended normally, it will emit 'finish'. But if not, the request is aborted by user for example, it will emit 'close', and I think it's emitted because the socket emits the 'close' event.

And as for 'stream' , there is: https://nodejs.org/docs/latest-v14.x/api/stream.html#stream_class_stream_writable

image

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@juanjoDiaz @tjpgt and others