diff --git a/lib/internal/streams/async_iterator.js b/lib/internal/streams/async_iterator.js index 5d7f3f0fb424b7..90960f5a3842e3 100644 --- a/lib/internal/streams/async_iterator.js +++ b/lib/internal/streams/async_iterator.js @@ -153,7 +153,7 @@ const createReadableStreamAsyncIterator = (stream) => { }); finished(stream, (err) => { - if (err) { + if (err && err.code !== 'ERR_STREAM_PREMATURE_CLOSE') { const reject = iterator[kLastReject]; // reject if we are waiting for data in the Promise // returned by next() and store the error diff --git a/test/parallel/test-stream-readable-async-iterators.js b/test/parallel/test-stream-readable-async-iterators.js index a9431fab81654a..83540de9defea3 100644 --- a/test/parallel/test-stream-readable-async-iterators.js +++ b/test/parallel/test-stream-readable-async-iterators.js @@ -335,11 +335,8 @@ async function tests() { readable.destroy(); - try { - await readable[Symbol.asyncIterator]().next(); - } catch (e) { - assert.strictEqual(e.code, 'ERR_STREAM_PREMATURE_CLOSE'); - } + const { done } = await readable[Symbol.asyncIterator]().next(); + assert.strictEqual(done, true); })(); await (async function() { @@ -380,6 +377,22 @@ async function tests() { for await (const b of r) { } })(); + + await (async () => { + console.log('destroy mid-stream does not error'); + const r = new Readable({ + objectMode: true, + read() { + this.push('asdf'); + this.push('hehe'); + } + }); + + // eslint-disable-next-line no-unused-vars + for await (const a of r) { + r.destroy(null); + } + })(); } // to avoid missing some tests if a promise does not resolve