Skip to content

Commit 3baa019

Browse files
committed
stream: avoid constructing extra promise in createPromiseCallback
1 parent 66054cc commit 3baa019

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

lib/internal/per_context/primordials.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,12 @@ function copyPrototype(src, dest, prefix) {
243243
copyPrototype(original.prototype, primordials, `${name}Prototype`);
244244
});
245245

246+
{
247+
const { Promise } = primordials;
248+
const OriginalPromiseTry = Promise.try;
249+
primordials.BindPromiseTry = (fn) => bind(OriginalPromiseTry, Promise, fn);
250+
}
251+
246252
// Create copies of abstract intrinsic objects that are not directly exposed
247253
// on the global object.
248254
// Refs: https://tc39.es/ecma262/#sec-%typedarray%-intrinsic-object

lib/internal/webstreams/util.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ const {
77
ArrayPrototypePush,
88
ArrayPrototypeShift,
99
AsyncIteratorPrototype,
10+
BindPromiseTry,
1011
MathMax,
1112
NumberIsNaN,
1213
PromisePrototypeThen,
14+
PromiseReject,
15+
PromiseResolve,
1316
ReflectApply,
1417
ReflectGet,
1518
Symbol,
@@ -164,7 +167,16 @@ function enqueueValueWithSize(controller, value, size) {
164167

165168
function createPromiseCallback(name, fn, thisArg) {
166169
validateFunction(fn, name);
167-
return async (...args) => ReflectApply(fn, thisArg, args);
170+
if (thisArg === undefined) {
171+
return BindPromiseTry(fn);
172+
}
173+
return (...args) => {
174+
try {
175+
return PromiseResolve(ReflectApply(fn, thisArg, args));
176+
} catch (err) {
177+
return PromiseReject(err);
178+
}
179+
};
168180
}
169181

170182
function isPromisePending(promise) {

0 commit comments

Comments
 (0)