Skip to content

Commit

Permalink
util: add internal createDeferredPromise()
Browse files Browse the repository at this point in the history
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.

PR-URL: #37095
Backport-PR-URL: #39704
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Zijian Liu <lxxyxzj@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
cjihrig authored and targos committed Aug 13, 2021
1 parent 606be49 commit 66f3f6a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
9 changes: 2 additions & 7 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ const {
ObjectAssign,
ObjectDefineProperty,
ObjectPrototypeHasOwnProperty,
Promise,
} = primordials;

const {
promisify,
convertToValidSignal,
createDeferredPromise,
getSystemErrorName
} = require('internal/util');
const { isArrayBufferView } = require('internal/util/types');
Expand Down Expand Up @@ -215,12 +215,7 @@ function exec(command, options, callback) {

const customPromiseExecFunction = (orig) => {
return (...args) => {
let resolve;
let reject;
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
const { promise, resolve, reject } = createDeferredPromise();

promise.child = orig(...args, (err, stdout, stderr) => {
if (err !== null) {
Expand Down
13 changes: 2 additions & 11 deletions lib/internal/blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const {
ArrayFrom,
ObjectSetPrototypeOf,
Promise,
PromiseResolve,
RegExpPrototypeTest,
StringPrototypeToLowerCase,
Expand Down Expand Up @@ -31,6 +30,7 @@ const {
} = require('internal/util/types');

const {
createDeferredPromise,
customInspectSymbol: kInspect,
emitExperimentalWarning,
} = require('internal/util');
Expand Down Expand Up @@ -58,15 +58,6 @@ const kLength = Symbol('kLength');

let Buffer;

function deferred() {
let res, rej;
const promise = new Promise((resolve, reject) => {
res = resolve;
rej = reject;
});
return { promise, resolve: res, reject: rej };
}

function lazyBuffer() {
if (Buffer === undefined)
Buffer = require('buffer').Buffer;
Expand Down Expand Up @@ -212,7 +203,7 @@ class Blob extends JSTransferable {
promise,
resolve,
reject
} = deferred();
} = createDeferredPromise();
job.ondone = (err, ab) => {
if (err !== undefined)
return reject(new AbortError());
Expand Down
12 changes: 12 additions & 0 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,11 +420,23 @@ function sleep(msec) {
_sleep(msec);
}

function createDeferredPromise() {
let resolve;
let reject;
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});

return { promise, resolve, reject };
}

module.exports = {
assertCrypto,
cachedResult,
convertToValidSignal,
createClassWrapper,
createDeferredPromise,
decorateErrorStack,
deprecate,
emitExperimentalWarning,
Expand Down

0 comments on commit 66f3f6a

Please sign in to comment.