Skip to content

Commit 014495a

Browse files
committed
util: add internal createInvertedPromise()
The pattern of resolving/rejecting a Promise from outside of its executor happens numerous times throughout the codebase (more than what is updated here in fact). This commit abstracts that logic into an internal utility function.
1 parent 7016c61 commit 014495a

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

lib/child_process.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ const {
3737
ObjectAssign,
3838
ObjectDefineProperty,
3939
ObjectPrototypeHasOwnProperty,
40-
Promise,
4140
RegExpPrototypeTest,
4241
SafeSet,
4342
StringPrototypeSlice,
@@ -47,6 +46,7 @@ const {
4746
const {
4847
promisify,
4948
convertToValidSignal,
49+
createInvertedPromise,
5050
getSystemErrorName
5151
} = require('internal/util');
5252
const { isArrayBufferView } = require('internal/util/types');
@@ -184,12 +184,7 @@ function exec(command, options, callback) {
184184

185185
const customPromiseExecFunction = (orig) => {
186186
return (...args) => {
187-
let resolve;
188-
let reject;
189-
const promise = new Promise((res, rej) => {
190-
resolve = res;
191-
reject = rej;
192-
});
187+
const { promise, resolve, reject } = createInvertedPromise();
193188

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

lib/internal/blob.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const {
44
ArrayFrom,
55
ObjectSetPrototypeOf,
6-
Promise,
76
PromiseResolve,
87
RegExpPrototypeTest,
98
StringPrototypeToLowerCase,
@@ -29,6 +28,7 @@ const {
2928
} = require('internal/util/types');
3029

3130
const {
31+
createInvertedPromise,
3232
customInspectSymbol: kInspect,
3333
emitExperimentalWarning,
3434
} = require('internal/util');
@@ -56,15 +56,6 @@ const kLength = Symbol('kLength');
5656

5757
let Buffer;
5858

59-
function deferred() {
60-
let res, rej;
61-
const promise = new Promise((resolve, reject) => {
62-
res = resolve;
63-
rej = reject;
64-
});
65-
return { promise, resolve: res, reject: rej };
66-
}
67-
6859
function lazyBuffer() {
6960
if (Buffer === undefined)
7061
Buffer = require('buffer').Buffer;
@@ -210,7 +201,7 @@ class Blob extends JSTransferable {
210201
promise,
211202
resolve,
212203
reject
213-
} = deferred();
204+
} = createInvertedPromise();
214205
job.ondone = (err, ab) => {
215206
if (err !== undefined)
216207
return reject(new AbortError());

lib/internal/util.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,11 +420,23 @@ function sleep(msec) {
420420
_sleep(msec);
421421
}
422422

423+
function createInvertedPromise() {
424+
let resolve;
425+
let reject;
426+
const promise = new Promise((res, rej) => {
427+
resolve = res;
428+
reject = rej;
429+
});
430+
431+
return { promise, resolve, reject };
432+
}
433+
423434
module.exports = {
424435
assertCrypto,
425436
cachedResult,
426437
convertToValidSignal,
427438
createClassWrapper,
439+
createInvertedPromise,
428440
decorateErrorStack,
429441
deprecate,
430442
emitExperimentalWarning,

0 commit comments

Comments
 (0)