-
-
Notifications
You must be signed in to change notification settings - Fork 968
Document response
event in stream docs
#1972
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
Conversation
|
||
const readStream = got.stream('http://example.com/image.png', {throwHttpErrors: false}); | ||
|
||
readStream.on('response', async response => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using async function for an event listener is an anti-pattern as any thrown error will become an unhandled error. Instead, wrap the async stuff in an async-IIFE and use try/catch to properly handle any error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there's a missing try/catch, there will be an unhandled error regardless whether it's an async function or an async IIFE.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hence, why I'm saying there should be a try/catch here. I think only pipeline
could throw here, so it should be:
(async () => {
try {
await pipeline(
readStream,
createWriteStream('image.png')
);
} catch {
// handle it
}
})();
Right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be one way. Another is:
readStream.on('response', async response => {
try {
await pipeline(
readStream,
createWriteStream('image.png')
);
} catch (error) {
// ...
}
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've modified the code. Should be fine now.
response
event in stream docs
I've added documentation in the form of an example around the got.stream response event.
Fixes #1955
Checklist