Skip to content

Commit f18db47

Browse files
committed
stream: remove isPromise utility function
The function was not checking if the parameter was actually a Promise instance, but if it has a `then` method. Removing the utility function in favor of a clearer `typeof` check, handling the case when the thenable throws if then method is accessed more than once.
1 parent 5dd344a commit f18db47

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

lib/internal/streams/pipeline.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66
const {
77
ArrayIsArray,
8+
ReflectApply,
89
SymbolAsyncIterator,
9-
SymbolIterator
10+
SymbolIterator,
1011
} = primordials;
1112

1213
let eos;
@@ -77,10 +78,6 @@ function popCallback(streams) {
7778
return streams.pop();
7879
}
7980

80-
function isPromise(obj) {
81-
return !!(obj && typeof obj.then === 'function');
82-
}
83-
8481
function isReadable(obj) {
8582
return !!(obj && typeof obj.pipe === 'function');
8683
}
@@ -222,14 +219,19 @@ function pipeline(...streams) {
222219
const pt = new PassThrough({
223220
objectMode: true
224221
});
225-
if (isPromise(ret)) {
226-
ret
227-
.then((val) => {
222+
223+
// Handle Promises/A+ spec, `then` could be a getter that throws on
224+
// second use.
225+
const then = ret?.then;
226+
if (typeof then === 'function') {
227+
ReflectApply(then, ret, [
228+
(val) => {
228229
value = val;
229230
pt.end(val);
230231
}, (err) => {
231232
pt.destroy(err);
232-
});
233+
}
234+
]);
233235
} else if (isIterable(ret, true)) {
234236
finishCount++;
235237
pump(ret, pt, finish);

0 commit comments

Comments
 (0)