From 66f3f6af65615adafbe3a805e4afde60810a0534 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Tue, 26 Jan 2021 23:15:56 -0500 Subject: [PATCH] util: add internal createDeferredPromise() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: https://github.com/nodejs/node/pull/37095 Backport-PR-URL: https://github.com/nodejs/node/pull/39704 Reviewed-By: Gus Caplan Reviewed-By: Luigi Pinca Reviewed-By: Michaƫl Zasso Reviewed-By: Zijian Liu Reviewed-By: James M Snell --- lib/child_process.js | 9 ++------- lib/internal/blob.js | 13 ++----------- lib/internal/util.js | 12 ++++++++++++ 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/lib/child_process.js b/lib/child_process.js index 09369316de52f1..8c10ce36cc7d74 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -28,12 +28,12 @@ const { ObjectAssign, ObjectDefineProperty, ObjectPrototypeHasOwnProperty, - Promise, } = primordials; const { promisify, convertToValidSignal, + createDeferredPromise, getSystemErrorName } = require('internal/util'); const { isArrayBufferView } = require('internal/util/types'); @@ -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) { diff --git a/lib/internal/blob.js b/lib/internal/blob.js index f0220552256737..accfdf9539c1d2 100644 --- a/lib/internal/blob.js +++ b/lib/internal/blob.js @@ -3,7 +3,6 @@ const { ArrayFrom, ObjectSetPrototypeOf, - Promise, PromiseResolve, RegExpPrototypeTest, StringPrototypeToLowerCase, @@ -31,6 +30,7 @@ const { } = require('internal/util/types'); const { + createDeferredPromise, customInspectSymbol: kInspect, emitExperimentalWarning, } = require('internal/util'); @@ -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; @@ -212,7 +203,7 @@ class Blob extends JSTransferable { promise, resolve, reject - } = deferred(); + } = createDeferredPromise(); job.ondone = (err, ab) => { if (err !== undefined) return reject(new AbortError()); diff --git a/lib/internal/util.js b/lib/internal/util.js index 9127c5a83e278e..be3a4eef644469 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -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,