File tree Expand file tree Collapse file tree 2 files changed +36
-6
lines changed Expand file tree Collapse file tree 2 files changed +36
-6
lines changed Original file line number Diff line number Diff line change @@ -706,14 +706,18 @@ function promisify(originalMethod, options) {
706
706
var slice = Array . prototype . slice ;
707
707
708
708
var wrapper = function ( ) {
709
- var args = slice . call ( arguments ) ;
710
- var hasCallback = is . fn ( args [ args . length - 1 ] ) ;
711
- var context = this ;
712
-
713
- if ( hasCallback ) {
714
- return originalMethod . apply ( context , args ) ;
709
+ var last ;
710
+ for ( last = arguments . length - 1 ; last >= 0 ; last -- ) {
711
+ var arg = arguments [ last ] ;
712
+ if ( arg === undefined ) continue ; // skip trailing undefined.
713
+ if ( ! is . fn ( arg ) ) break ; // non-callback last argument found.
714
+ return originalMethod . apply ( this , arguments ) ;
715
715
}
716
716
717
+ // peel trailing undefined.
718
+ var args = slice . call ( arguments , 0 , last + 1 ) ;
719
+ var context = this ;
720
+
717
721
var PromiseCtor = Promise ;
718
722
719
723
// Because dedupe will likely create a single install of
Original file line number Diff line number Diff line change @@ -1648,6 +1648,32 @@ describe('common/util', function() {
1648
1648
1649
1649
assert . strictEqual ( FakeClass . prototype . methodName , method ) ;
1650
1650
} ) ;
1651
+
1652
+ describe ( 'trailing undefined arguments' , function ( ) {
1653
+ it ( 'should not return a promise in callback mode' , function ( done ) {
1654
+ var func = util . promisify ( function ( optional , callback ) {
1655
+ assert ( is . fn ( optional ) ) ;
1656
+ optional ( null ) ;
1657
+ } ) ;
1658
+
1659
+ var returnVal = func ( function ( ) {
1660
+ assert ( ! returnVal ) ;
1661
+ done ( ) ;
1662
+ } ) ;
1663
+ } ) ;
1664
+
1665
+ it ( 'should return a promise when callback omitted' , function ( done ) {
1666
+ var func = util . promisify ( function ( optional , callback ) {
1667
+ assert . strictEqual ( arguments . length , 1 ) ;
1668
+ assert ( is . fn ( optional ) ) ;
1669
+ optional ( null ) ;
1670
+ } ) ;
1671
+
1672
+ var returnVal = func ( undefined , undefined ) . then ( function ( ) {
1673
+ done ( ) ;
1674
+ } ) ;
1675
+ } ) ;
1676
+ } ) ;
1651
1677
} ) ;
1652
1678
1653
1679
describe ( 'promisify' , function ( ) {
You can’t perform that action at this time.
0 commit comments