-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Open
Description
I've noticed that for await..of loops occasionally hang when you return early and you don't read the response body to completion:
async function parse (source) {
for await (const buf of source) {
if (... some criteria) {
// exit early
return 'some value'
}
}
}
const res = fetch('http://...')
const result = await parse(res.body) // this promise *sometimes* never resolves or rejectsres.body here is a PassThrough stream - the workaround is to wrap the async iterator it produces and destroy the stream when the return method is called, but it only seems to be necessary if the writable side has ended:
const res = fetch('http://...')
const iter = res.body[Symbol.asyncIterator]()
const wrapped = {
next: iter.next.bind(iter),
return: () => {
if (res.body.writableEnded) {
res.body.destroy()
}
return iter.return()
}
}
const result = await parse(wrapped) // now always resolvesCan this be handled by node-fetch or is this a problem with node itself?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels