Open meganwalker-ibm opened 2 years ago
I've observed this as well. It worked in earlier versions of Got (I think 10.x), but is currently not working without an extra await
.
I had this code:
const source = client.stream('https://example.com/')
for await (const chunk of source) {
console.log(chunk)
}
Which now results in:
TypeError: data is not async iterable
Adding await
to the return value of client.stream
fixes it:
const source = await client.stream('https://example.com/')
for await (const chunk of source) {
console.log(chunk)
}
Describe the bug
got.stream seems to return an undocumented Promise in some circumstances. This is evident when you pass invalid Options that fail to be validated by got, which then triggers a Rejected Promise to be returned to the user. The Documentation only mentions that got.stream returns a Duplex.
Actual behavior
An undocumented Promise is returned and Rejected due to the incorrect options, this can cause
UnhandledPromiseRejectionWarning
if the user does not realise that a Promise can be returned fromgot.stream
...
Expected behavior
Either a - the Promise response is documented for got.stream(), making clear the Request it returns can be a Promise. b - the rejected Promise is caught by got, and is converted into calling the stream's .on('error', <...>) handler
Now we know that it returns a promise in some circumstances despite the user asking for a stream, documenting would be sufficient to resolve this; though I'm not 100% sure if that's the 'right' thing to do, when the user has asked for a stream instead. ...
Code to reproduce
This prints out 'promise error'. Failing to make use of the undocumented .catch() function from the Promise means you're likely to get an UnhandledPromiseRejectionWarning unless you have code to catch those more generally. It does not print the 'stream error' line, nor do any event handlers get called.
Checklist