Commit ccca547 1 parent 553169f commit ccca547 Copy full SHA for ccca547
File tree 3 files changed +32
-3
lines changed
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.
3387
3387
3388
3388
<!-- YAML
3389
3389
changes:
3390
+ - version: REPLACEME
3391
+ pr-url: https://github.com/nodejs/node/pull/49609
3392
+ description: Runtime deprecation.
3390
3393
- version: REPLACEME
3391
3394
pr-url: https://github.com/nodejs/node/pull/49647
3392
3395
description: Documentation-only deprecation.
3393
3396
-->
3394
3397
3395
- Type: Documentation-only
3398
+ Type: Runtime
3396
3399
3397
3400
Calling [ ` util.promisify ` ] [ ] on a function that returns a <Promise > will ignore
3398
3401
the 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 {
63
63
sleep : _sleep ,
64
64
toUSVString : _toUSVString ,
65
65
} = internalBinding ( 'util' ) ;
66
- const { isNativeError } = internalBinding ( 'types' ) ;
66
+ const { isNativeError, isPromise } = internalBinding ( 'types' ) ;
67
67
const { getOptionValue } = require ( 'internal/options' ) ;
68
68
69
69
const noCrypto = ! process . versions . openssl ;
@@ -409,7 +409,10 @@ function promisify(original) {
409
409
resolve ( values [ 0 ] ) ;
410
410
}
411
411
} ) ;
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
+ }
413
416
} ) ;
414
417
}
415
418
Original file line number Diff line number Diff line change @@ -7,6 +7,29 @@ const vm = require('vm');
7
7
const { promisify } = require ( 'util' ) ;
8
8
const { customPromisifyArgs } = require ( 'internal/util' ) ;
9
9
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
+
10
33
const stat = promisify ( fs . stat ) ;
11
34
12
35
{
You can’t perform that action at this time.
0 commit comments