Skip to content

Commit edd38e6

Browse files
committed
Clean up of unhandled rejection code.
Substantive changes: - Exposes the unhandled rejection reasons as `Q.unhandledReasons`. - Uses "reason" more consistently, in place of "exception" and "error". - Changes the `console.log` message to be something a bit more explicit than "should be empty".
1 parent 34a7b41 commit edd38e6

File tree

1 file changed

+35
-31
lines changed

1 file changed

+35
-31
lines changed

q.js

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -657,68 +657,72 @@ function isRejected(object) {
657657
return isPromise(object) && "exception" in object;
658658
}
659659

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() {
664668
if (
665-
!errorsDisplayed &&
669+
!unhandledReasonsDisplayed &&
666670
typeof window !== "undefined" &&
667671
!window.Touch &&
668672
window.console
669673
) {
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);
675676
}
676-
errorsDisplayed = true;
677+
678+
unhandledReasonsDisplayed = true;
677679
}
678680

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.)
682684
if (typeof process !== "undefined" && process.on) {
683685
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);
688690
} else {
689-
console.warn("Unhandled rejected promise (no stack):", error);
691+
console.warn("Unhandled rejection reason (no stack):", reason);
690692
}
691693
}
692694
});
693695
}
694696

695697
/**
696698
* Constructs a rejected promise.
697-
* @param exception value describing the failure
699+
* @param reason value describing the failure
698700
*/
699701
Q.reject = reject;
700-
function reject(exception) {
702+
function reject(reason) {
701703
var rejection = makePromise({
702704
"when": function (rejected) {
703705
// note that the error has been handled
704706
if (rejected) {
705-
var at = array_indexOf(rejections, this);
707+
var at = array_indexOf(unhandledRejections, this);
706708
if (at !== -1) {
707-
errors.splice(at, 1);
708-
rejections.splice(at, 1);
709+
unhandledRejections.splice(at, 1);
710+
unhandledReasons.splice(at, 1);
709711
}
710712
}
711-
return rejected ? rejected(exception) : this;
713+
return rejected ? rejected(reason) : this;
712714
}
713715
}, function fallback() {
714-
return reject(exception);
716+
return reject(reason);
715717
}, function valueOf() {
716718
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+
722726
return rejection;
723727
}
724728

0 commit comments

Comments
 (0)