From 4a9511d971596922038e0a5cbc3e6f6829352a11 Mon Sep 17 00:00:00 2001 From: LiviaMedeiros Date: Fri, 27 May 2022 00:04:09 +0800 Subject: [PATCH] lib: give names to promisified methods Affected functions: `fs.exists`, `readline.Interface.prototype.question` PR-URL: https://github.com/nodejs/node/pull/43218 Reviewed-By: Luigi Pinca Reviewed-By: Minwoo Jung --- lib/fs.js | 4 +- lib/readline.js | 2 +- .../test-util-promisify-custom-names.mjs | 40 +++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 test/parallel/test-util-promisify-custom-names.mjs diff --git a/lib/fs.js b/lib/fs.js index b251761bd420a2..a3f72535d77554 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -278,9 +278,9 @@ function exists(path, callback) { ObjectDefineProperty(exists, internalUtil.promisify.custom, { __proto__: null, - value: (path) => { + value: function exists(path) { // eslint-disable-line func-name-matching return new Promise((resolve) => fs.exists(path, resolve)); - } + }, }); // fs.existsSync never throws, it only returns true or false. diff --git a/lib/readline.js b/lib/readline.js index 0957de58506a66..82afaa285d67ad 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -153,7 +153,7 @@ Interface.prototype.question = function(query, options, cb) { FunctionPrototypeCall(superQuestion, this, query, cb); } }; -Interface.prototype.question[promisify.custom] = function(query, options) { +Interface.prototype.question[promisify.custom] = function question(query, options) { options = typeof options === 'object' && options !== null ? options : {}; if (options.signal && options.signal.aborted) { diff --git a/test/parallel/test-util-promisify-custom-names.mjs b/test/parallel/test-util-promisify-custom-names.mjs new file mode 100644 index 00000000000000..3ff05d907b5060 --- /dev/null +++ b/test/parallel/test-util-promisify-custom-names.mjs @@ -0,0 +1,40 @@ +import '../common/index.mjs'; +import assert from 'node:assert'; +import { promisify } from 'node:util'; + +// Test that customly promisified methods in [util.promisify.custom] +// have appropriate names + +import fs from 'node:fs'; +import readline from 'node:readline'; +import stream from 'node:stream'; +import timers from 'node:timers'; + + +assert.strictEqual( + promisify(fs.exists).name, + 'exists' +); + +assert.strictEqual( + promisify(readline.Interface.prototype.question).name, + 'question', +); + +assert.strictEqual( + promisify(stream.finished).name, + 'finished' +); +assert.strictEqual( + promisify(stream.pipeline).name, + 'pipeline' +); + +assert.strictEqual( + promisify(timers.setImmediate).name, + 'setImmediate' +); +assert.strictEqual( + promisify(timers.setTimeout).name, + 'setTimeout' +);