Skip to content

Commit

Permalink
Merge pull request #1464 from nornagon/promise-rejection-event
Browse files Browse the repository at this point in the history
Conform to spec when generating PromiseRejectionEvent
  • Loading branch information
petkaantonov authored Oct 6, 2017
2 parents a6abe60 + 0da7d15 commit b7f2139
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/debuggability.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var getDomain = Promise._getDomain;
var async = Promise._async;
var Warning = require("./errors").Warning;
var util = require("./util");
var es5 = require("./es5");
var ASSERT = require("./assert");
var canAttachTrace = util.canAttachTrace;
var unhandledRejectionHandled;
Expand Down Expand Up @@ -154,10 +155,14 @@ var fireDomEvent = (function() {
var event = new CustomEvent("CustomEvent");
util.global.dispatchEvent(event);
return function(name, event) {
var domEvent = new CustomEvent(name.toLowerCase(), {
var eventData = {
detail: event,
cancelable: true
});
};
es5.defineProperty(
eventData, "promise", {value: event.promise});
es5.defineProperty(eventData, "reason", {value: event.reason});
var domEvent = new CustomEvent(name.toLowerCase(), eventData);
return !util.global.dispatchEvent(domEvent);
};
// In Firefox < 48 CustomEvent is not available in workers but
Expand All @@ -170,6 +175,8 @@ var fireDomEvent = (function() {
cancelable: true
});
domEvent.detail = event;
es5.defineProperty(domEvent, "promise", {value: event.promise});
es5.defineProperty(domEvent, "reason", {value: event.reason});
return !util.global.dispatchEvent(domEvent);
};
} else {
Expand Down
8 changes: 8 additions & 0 deletions test/mocha/unhandled_rejections.js
Original file line number Diff line number Diff line change
Expand Up @@ -644,23 +644,31 @@ if (windowDomEventSupported) {
e.preventDefault();
assert.strictEqual(e.detail.promise, promise);
assert.strictEqual(e.detail.reason, err);
assert.strictEqual(e.promise, promise);
assert.strictEqual(e.reason, err);
order.push(1);
});
attachEvent("unhandledrejection", function(e) {
assert.strictEqual(e.detail.promise, promise);
assert.strictEqual(e.detail.reason, err);
assert.strictEqual(e.promise, promise);
assert.strictEqual(e.reason, err);
assert.strictEqual(e.defaultPrevented, true);
order.push(2);
});
attachEvent("rejectionhandled", function(e) {
e.preventDefault();
assert.strictEqual(e.detail.promise, promise);
assert.strictEqual(e.detail.reason, undefined);
assert.strictEqual(e.promise, promise);
assert.strictEqual(e.reason, err);
order.push(3);
});
attachEvent("rejectionhandled", function(e) {
assert.strictEqual(e.detail.promise, promise);
assert.strictEqual(e.detail.reason, undefined);
assert.strictEqual(e.promise, promise);
assert.strictEqual(e.reason, err);
assert.strictEqual(e.defaultPrevented, true);
order.push(4);
resolve();
Expand Down

0 comments on commit b7f2139

Please sign in to comment.