From 3187845980956e88185edf377b3990fd50271d66 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 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 02a12010c5ca56..74699a16834c30 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -37,7 +37,6 @@ const { ObjectAssign, ObjectDefineProperty, ObjectPrototypeHasOwnProperty, - Promise, RegExpPrototypeTest, SafeSet, StringPrototypeSlice, @@ -47,6 +46,7 @@ const { const { promisify, convertToValidSignal, + createDeferredPromise, getSystemErrorName } = require('internal/util'); const { isArrayBufferView } = require('internal/util/types'); @@ -185,12 +185,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 e49b3710e0768a..82b375da783fe1 100644 --- a/lib/internal/blob.js +++ b/lib/internal/blob.js @@ -3,7 +3,6 @@ const { ArrayFrom, ObjectSetPrototypeOf, - Promise, PromiseResolve, RegExpPrototypeTest, StringPrototypeToLowerCase, @@ -29,6 +28,7 @@ const { } = require('internal/util/types'); const { + createDeferredPromise, customInspectSymbol: kInspect, emitExperimentalWarning, } = require('internal/util'); @@ -56,15 +56,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; @@ -210,7 +201,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 5d5a2b14a708b4..46eabc6f08c092 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,