From c544a71111dd982d83c303c29802c2a92266d931 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Tue, 26 Jan 2021 23:15:56 -0500 Subject: [PATCH] util: add internal createDeferredPromise() 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. --- 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 f51072126e21cd6..7c3cc8e72e0e60c 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'); @@ -184,12 +184,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 e49b3710e0768a7..82b375da783fe1c 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 5d5a2b14a708b45..46eabc6f08c092b 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,