@@ -657,68 +657,72 @@ function isRejected(object) {
657
657
return isPromise ( object ) && "exception" in object ;
658
658
}
659
659
660
- var rejections = [ ] ;
661
- var errors = [ ] ;
662
- var errorsDisplayed ;
663
- function displayErrors ( ) {
660
+ // This promise library consumes exceptions thrown in handlers so they can be
661
+ // handled by a subsequent promise. The exceptions get added to this array when
662
+ // they are created, and removed when they are handled. Note that in ES6 or
663
+ // shimmed environments, this would naturally be a `Set`.
664
+ var unhandledReasons = Q . unhandledReasons = [ ] ;
665
+ var unhandledRejections = [ ] ;
666
+ var unhandledReasonsDisplayed = false ;
667
+ function displayUnhandledReasons ( ) {
664
668
if (
665
- ! errorsDisplayed &&
669
+ ! unhandledReasonsDisplayed &&
666
670
typeof window !== "undefined" &&
667
671
! window . Touch &&
668
672
window . console
669
673
) {
670
- // This promise library consumes exceptions thrown in handlers so
671
- // they can be handled by a subsequent promise. The rejected
672
- // promises get added to this array when they are created, and
673
- // removed when they are handled.
674
- console . log ( "Should be empty:" , errors ) ;
674
+ console . warn ( "[Q] Unhandled rejection reasons (should be empty):" ,
675
+ unhandledReasons ) ;
675
676
}
676
- errorsDisplayed = true ;
677
+
678
+ unhandledReasonsDisplayed = true ;
677
679
}
678
680
679
- // Show unhandled rejection if Node exits without handling an outstanding
680
- // rejection. (Note that Browserify presently produces a process global
681
- // without the Emitter on interface )
681
+ // Show unhandled rejection reasons if Node exits without handling an
682
+ // outstanding rejection. (Note that Browserify presently produces a process
683
+ // global without the `EventEmitter` `on` method. )
682
684
if ( typeof process !== "undefined" && process . on ) {
683
685
process . on ( "exit" , function ( ) {
684
- for ( var i = 0 ; i < errors . length ; i ++ ) {
685
- var error = errors [ i ] ;
686
- if ( error && typeof error . stack !== "undefined" ) {
687
- console . warn ( "Unhandled rejected promise :" , error . stack ) ;
686
+ for ( var i = 0 ; i < unhandledReasons . length ; i ++ ) {
687
+ var reason = unhandledReasons [ i ] ;
688
+ if ( reason && typeof reason . stack !== "undefined" ) {
689
+ console . warn ( "Unhandled rejection reason :" , reason . stack ) ;
688
690
} else {
689
- console . warn ( "Unhandled rejected promise (no stack):" , error ) ;
691
+ console . warn ( "Unhandled rejection reason (no stack):" , reason ) ;
690
692
}
691
693
}
692
694
} ) ;
693
695
}
694
696
695
697
/**
696
698
* Constructs a rejected promise.
697
- * @param exception value describing the failure
699
+ * @param reason value describing the failure
698
700
*/
699
701
Q . reject = reject ;
700
- function reject ( exception ) {
702
+ function reject ( reason ) {
701
703
var rejection = makePromise ( {
702
704
"when" : function ( rejected ) {
703
705
// note that the error has been handled
704
706
if ( rejected ) {
705
- var at = array_indexOf ( rejections , this ) ;
707
+ var at = array_indexOf ( unhandledRejections , this ) ;
706
708
if ( at !== - 1 ) {
707
- errors . splice ( at , 1 ) ;
708
- rejections . splice ( at , 1 ) ;
709
+ unhandledRejections . splice ( at , 1 ) ;
710
+ unhandledReasons . splice ( at , 1 ) ;
709
711
}
710
712
}
711
- return rejected ? rejected ( exception ) : this ;
713
+ return rejected ? rejected ( reason ) : this ;
712
714
}
713
715
} , function fallback ( ) {
714
- return reject ( exception ) ;
716
+ return reject ( reason ) ;
715
717
} , function valueOf ( ) {
716
718
return this ;
717
- } , exception , true ) ;
718
- // note that the error has not been handled
719
- displayErrors ( ) ;
720
- rejections . push ( rejection ) ;
721
- errors . push ( exception ) ;
719
+ } , reason , true ) ;
720
+
721
+ // Note that the reason has not been handled.
722
+ displayUnhandledReasons ( ) ;
723
+ unhandledRejections . push ( rejection ) ;
724
+ unhandledReasons . push ( reason ) ;
725
+
722
726
return rejection ;
723
727
}
724
728
0 commit comments