Skip to content

Commit

Permalink
cehck the return type instead
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Sep 13, 2023
1 parent 954d2d0 commit d28fd83
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 18 deletions.
1 change: 0 additions & 1 deletion lib/internal/per_context/primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@ primordials.SymbolAsyncDispose ??= primordials.SymbolFor('nodejs.asyncDispose');
// on the global object.
// Refs: https://tc39.es/ecma262/#sec-%typedarray%-intrinsic-object
[
{ name: 'AsyncFunction', original: async function() {}.constructor },
{ name: 'TypedArray', original: Reflect.getPrototypeOf(Uint8Array) },
{ name: 'ArrayIterator', original: {
prototype: Reflect.getPrototypeOf(Array.prototype[Symbol.iterator]()),
Expand Down
17 changes: 4 additions & 13 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const {
ArrayPrototypePush,
ArrayPrototypeSlice,
ArrayPrototypeSort,
AsyncFunction,
Error,
FunctionPrototypeCall,
ObjectDefineProperties,
Expand Down Expand Up @@ -64,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 @@ -391,16 +390,6 @@ function promisify(original) {
});
}

try {
const { constructor } = original;
if (constructor === AsyncFunction) {
process.emitWarning('Calling promisify on async functions is likely a mistake.');
}
} catch {
// If `constructor` was a getter that throws, ignore the error.
}


// Names to create an object from in case the callback receives multiple
// arguments, e.g. ['bytesRead', 'buffer'] for fs.read.
const argumentNames = original[kCustomPromisifyArgsSymbol];
Expand All @@ -420,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
11 changes: 7 additions & 4 deletions test/parallel/test-util-promisify.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ const vm = require('vm');
const { promisify } = require('util');
const { customPromisifyArgs } = require('internal/util');

common.expectWarning('Warning', 'Calling promisify on async functions is likely a mistake.');
promisify(async () => {});

{
const warningHandler = common.mustNotCall();
process.on('warning', warningHandler);
function foo() {}
foo.constructor = { name: 'AsyncFunction' };
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 d28fd83

Please sign in to comment.