Skip to content

Commit 27651d3

Browse files
authored
refactor(ext/node): use Promise.withResolvers (#28917)
1 parent 9b2c02f commit 27651d3

File tree

3 files changed

+16
-37
lines changed

3 files changed

+16
-37
lines changed

ext/node/polyfills/child_process.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// TODO(petamoriken): enable prefer-primordials for node polyfills
77
// deno-lint-ignore-file prefer-primordials
88

9-
import { internals } from "ext:core/mod.js";
9+
import { internals, primordials } from "ext:core/mod.js";
1010
import {
1111
op_bootstrap_unstable_args,
1212
op_node_child_ipc_pipe,
@@ -37,16 +37,7 @@ import {
3737
ERR_OUT_OF_RANGE,
3838
genericNodeError,
3939
} from "ext:deno_node/internal/errors.ts";
40-
import {
41-
ArrayIsArray,
42-
ArrayPrototypeJoin,
43-
ArrayPrototypePush,
44-
ArrayPrototypeSlice,
45-
ObjectAssign,
46-
StringPrototypeSlice,
47-
} from "ext:deno_node/internal/primordials.mjs";
4840
import { getSystemErrorName, promisify } from "node:util";
49-
import { createDeferredPromise } from "ext:deno_node/internal/util.mjs";
5041
import process from "node:process";
5142
import { Buffer } from "node:buffer";
5243
import {
@@ -55,6 +46,16 @@ import {
5546
} from "ext:deno_node/internal/util.mjs";
5647
import { kNeedsNpmProcessState } from "ext:deno_process/40_process.js";
5748

49+
const {
50+
ArrayIsArray,
51+
ArrayPrototypeJoin,
52+
ArrayPrototypePush,
53+
ArrayPrototypeSlice,
54+
ObjectAssign,
55+
PromiseWithResolvers,
56+
StringPrototypeSlice,
57+
} = primordials;
58+
5859
const MAX_BUFFER = 1024 * 1024;
5960

6061
type ForkOptions = ChildProcessOptions;
@@ -345,11 +346,7 @@ type ExecExceptionForPromisify = ExecException & ExecOutputForPromisify;
345346

346347
const customPromiseExecFunction = (orig: typeof exec) => {
347348
return (...args: [command: string, options: ExecOptions]) => {
348-
const { promise, resolve, reject } = createDeferredPromise() as unknown as {
349-
promise: PromiseWithChild<ExecOutputForPromisify>;
350-
resolve?: (value: ExecOutputForPromisify) => void;
351-
reject?: (reason?: ExecExceptionForPromisify) => void;
352-
};
349+
const { promise, resolve, reject } = PromiseWithResolvers();
353350

354351
promise.child = orig(...args, (err, stdout, stderr) => {
355352
if (err !== null) {
@@ -681,11 +678,7 @@ const customPromiseExecFileFunction = (
681678
options?: ExecFileOptions,
682679
]
683680
) => {
684-
const { promise, resolve, reject } = createDeferredPromise() as unknown as {
685-
promise: PromiseWithChild<ExecOutputForPromisify>;
686-
resolve?: (value: ExecOutputForPromisify) => void;
687-
reject?: (reason?: ExecFileExceptionForPromisify) => void;
688-
};
681+
const { promise, resolve, reject } = PromiseWithResolvers();
689682

690683
promise.child = orig(...args, (err, stdout, stderr) => {
691684
if (err !== null) {

ext/node/polyfills/internal/util.mjs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,6 @@ export function once(callback) {
3535
};
3636
}
3737

38-
export function createDeferredPromise() {
39-
let resolve;
40-
let reject;
41-
const promise = new Promise((res, rej) => {
42-
resolve = res;
43-
reject = rej;
44-
});
45-
46-
return { promise, resolve, reject };
47-
}
48-
4938
// In addition to being accessible through util.promisify.custom,
5039
// this symbol is registered globally and can be accessed in any environment as
5140
// Symbol.for('nodejs.util.promisify.custom').
@@ -175,7 +164,6 @@ promisify.custom = kCustomPromisifiedSymbol;
175164

176165
export default {
177166
convertToValidSignal,
178-
createDeferredPromise,
179167
customInspectSymbol,
180168
customPromisifyArgs,
181169
kEmptyObject,

ext/node/polyfills/util.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,10 @@ const {
2828
StringPrototypePadStart,
2929
StringPrototypeToWellFormed,
3030
PromiseResolve,
31+
PromiseWithResolvers,
3132
} = primordials;
3233

33-
import {
34-
createDeferredPromise,
35-
promisify,
36-
} from "ext:deno_node/internal/util.mjs";
34+
import { promisify } from "ext:deno_node/internal/util.mjs";
3735
import { callbackify } from "ext:deno_node/_util/_util_callbackify.js";
3836
import { debuglog } from "ext:deno_node/internal/util/debuglog.ts";
3937
import {
@@ -317,7 +315,7 @@ export async function aborted(
317315
if (signal.aborted) {
318316
return PromiseResolve();
319317
}
320-
const abortPromise = createDeferredPromise();
318+
const abortPromise = PromiseWithResolvers();
321319
signal[abortSignal.add](abortPromise.resolve);
322320
return abortPromise.promise;
323321
}

0 commit comments

Comments
 (0)