Skip to content

Commit

Permalink
util: runtime deprecate promisify-ing a function returning a Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Sep 16, 2023
1 parent 71b90fa commit 2fb3d51
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const {
sleep: _sleep,
toUSVString: _toUSVString,
} = internalBinding('util');
const { isNativeError } = internalBinding('types');
const { isNativeError, isPromise } = internalBinding('types');
const { getOptionValue } = require('internal/options');

const noCrypto = !process.versions.openssl;
Expand Down Expand Up @@ -409,7 +409,9 @@ function promisify(original) {
resolve(values[0]);
}
});
ReflectApply(original, this, args);
if (isPromise(ReflectApply(original, this, args))) {
process.emitWarning('Calling promisify on a function that returns a Promise is likely a mistake.');
}
});
}

Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-util-promisify.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ const vm = require('vm');
const { promisify } = require('util');
const { customPromisifyArgs } = require('internal/util');

{
const warningHandler = common.mustNotCall();
process.on('warning', warningHandler);
function foo() {}
foo.constructor = (async () => {}).constructor;
promisify(foo);
process.off('warning', warningHandler);
}

common.expectWarning('Warning', 'Calling promisify on a function that returns a Promise is likely a mistake.');
promisify(async (callback) => { callback(); })().then(common.mustCall(() => {
common.expectWarning('Warning', 'Calling promisify on a function that returns a Promise is likely a mistake.');
promisify(async () => {})().then(common.mustNotCall());
}));

const stat = promisify(fs.stat);

{
Expand Down

0 comments on commit 2fb3d51

Please sign in to comment.