Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/internal/streams/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,7 @@ function streamToAsyncIterator(stream, options) {

async function* createAsyncIterator(stream, options) {
let callback = nop;
let isListening = false;

function next(resolve) {
if (this === stream) {
Expand All @@ -1390,15 +1391,18 @@ async function* createAsyncIterator(stream, options) {
}
}

stream.on('readable', next);

let error;
const cleanup = eos(stream, { writable: false }, (err) => {
error = err ? aggregateTwoErrors(error, err) : null;
callback();
callback = nop;
});

function setupReadable() {
isListening = true;
stream.on('readable', next);
}

try {
while (true) {
const chunk = stream.destroyed ? null : stream.read();
Expand All @@ -1409,6 +1413,9 @@ async function* createAsyncIterator(stream, options) {
} else if (error === null) {
return;
} else {
if (!isListening) {
setupReadable();
}
await new Promise(next);
}
}
Expand Down
Loading