File tree Expand file tree Collapse file tree 3 files changed +32
-3
lines changed Expand file tree Collapse file tree 3 files changed +32
-3
lines changed Original file line number Diff line number Diff line change @@ -3387,12 +3387,15 @@ Consider using alternatives such as the [`mock`][] helper function.
33873387
33883388<!-- YAML
33893389changes:
3390+ - version: REPLACEME
3391+ pr-url: https://github.com/nodejs/node/pull/49609
3392+ description: Runtime deprecation.
33903393 - version: REPLACEME
33913394 pr-url: https://github.com/nodejs/node/pull/49647
33923395 description: Documentation-only deprecation.
33933396-->
33943397
3395- Type: Documentation-only
3398+ Type: Runtime
33963399
33973400Calling [ ` util.promisify ` ] [ ] on a function that returns a <Promise > will ignore
33983401the result of said promise, which can lead to unhandled promise rejections.
Original file line number Diff line number Diff line change @@ -63,7 +63,7 @@ const {
6363 sleep : _sleep ,
6464 toUSVString : _toUSVString ,
6565} = internalBinding ( 'util' ) ;
66- const { isNativeError } = internalBinding ( 'types' ) ;
66+ const { isNativeError, isPromise } = internalBinding ( 'types' ) ;
6767const { getOptionValue } = require ( 'internal/options' ) ;
6868
6969const noCrypto = ! process . versions . openssl ;
@@ -409,7 +409,10 @@ function promisify(original) {
409409 resolve ( values [ 0 ] ) ;
410410 }
411411 } ) ;
412- ReflectApply ( original , this , args ) ;
412+ if ( isPromise ( ReflectApply ( original , this , args ) ) ) {
413+ process . emitWarning ( 'Calling promisify on a function that returns a Promise is likely a mistake.' ,
414+ 'DeprecationWarning' , 'DEP0174' ) ;
415+ }
413416 } ) ;
414417 }
415418
Original file line number Diff line number Diff line change @@ -7,6 +7,29 @@ const vm = require('vm');
77const { promisify } = require ( 'util' ) ;
88const { customPromisifyArgs } = require ( 'internal/util' ) ;
99
10+ {
11+ const warningHandler = common . mustNotCall ( ) ;
12+ process . on ( 'warning' , warningHandler ) ;
13+ function foo ( ) { }
14+ foo . constructor = ( async ( ) => { } ) . constructor ;
15+ promisify ( foo ) ;
16+ process . off ( 'warning' , warningHandler ) ;
17+ }
18+
19+ common . expectWarning (
20+ 'DeprecationWarning' ,
21+ 'Calling promisify on a function that returns a Promise is likely a mistake.' ,
22+ 'DEP0174' ) ;
23+ promisify ( async ( callback ) => { callback ( ) ; } ) ( ) . then ( common . mustCall ( ( ) => {
24+ // We must add the second `expectWarning` call in the `.then` handler, when
25+ // the first warning has already been triggered.
26+ common . expectWarning (
27+ 'DeprecationWarning' ,
28+ 'Calling promisify on a function that returns a Promise is likely a mistake.' ,
29+ 'DEP0174' ) ;
30+ promisify ( async ( ) => { } ) ( ) . then ( common . mustNotCall ( ) ) ;
31+ } ) ) ;
32+
1033const stat = promisify ( fs . stat ) ;
1134
1235{
You can’t perform that action at this time.
0 commit comments