Skip to content

Commit 4aadf03

Browse files
committed
stream: resolve stream/iter push() return() with its value
The iterator of a push() readable resolved return(value) with an undefined value, unlike async generators and the other stream/iter iterators. Since push() readables are no longer wrapped by from(), this also applied to from() and pipeTo(). Resolve it with `value`. Assisted-by: OpenCode Signed-off-by: James M Snell <jasnell@gmail.com>
1 parent 090f00f commit 4aadf03

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎lib/internal/streams/iter/push.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,9 +745,9 @@ function createReadable(queue) {
745745
async next() {
746746
return queue.read();
747747
},
748-
async return() {
748+
async return(value) {
749749
queue.consumerReturn();
750-
return new IterResult(true, undefined);
750+
return new IterResult(true, value);
751751
},
752752
async throw(error) {
753753
queue.consumerThrow(error);

‎test/parallel/test-stream-iter-push-basic.js‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
const common = require('../common');
55
const assert = require('assert');
6-
const { push, text } = require('stream/iter');
6+
const { from, push, text } = require('stream/iter');
77

88
async function testBasicWriteRead() {
99
const { writer, readable } = push();
@@ -174,6 +174,21 @@ async function testInvalidBackpressure() {
174174
}
175175
}
176176

177+
async function testReturnValue() {
178+
// return(value) resolves with `value`, read directly or through from().
179+
for (const getIterator of [
180+
(readable) => readable[Symbol.asyncIterator](),
181+
(readable) => from(readable)[Symbol.asyncIterator](),
182+
]) {
183+
const { writer, readable } = push();
184+
const iterator = getIterator(readable);
185+
const result = await iterator.return('value');
186+
assert.strictEqual(result.done, true);
187+
assert.strictEqual(result.value, 'value');
188+
assert.strictEqual(writer.canWrite, null);
189+
}
190+
}
191+
177192
Promise.all([
178193
testBasicWriteRead(),
179194
testMultipleWrites(),
@@ -187,4 +202,5 @@ Promise.all([
187202
testConsumerBreakWriteSyncReturnsFalse(),
188203
testPushWithTransforms(),
189204
testInvalidBackpressure(),
205+
testReturnValue(),
190206
]).then(common.mustCall());

0 commit comments

Comments
 (0)