Skip to content

Commit

Permalink
lib: give names to promisified methods
Browse files Browse the repository at this point in the history
Affected functions: `fs.exists`, `readline.Interface.prototype.question`

PR-URL: #43218
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
  • Loading branch information
LiviaMedeiros authored and danielleadams committed Jun 12, 2022
1 parent d58f273 commit bb9a7a9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
40 changes: 40 additions & 0 deletions test/parallel/test-util-promisify-custom-names.mjs
Original file line number Diff line number Diff line change
@@ -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'
);

0 comments on commit bb9a7a9

Please sign in to comment.