Skip to content

Commit b0fadff

Browse files
committed
Allows an inheriting constructor to return itself when calling .then
Signed-off-by: Christopher Rogers <[email protected]>
1 parent bdea8dd commit b0fadff

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function Promise(fn) {
1212
var self = this
1313

1414
this.then = function(onFulfilled, onRejected) {
15-
return new Promise(function(resolve, reject) {
15+
return new self.constructor(function(resolve, reject) {
1616
handle(new Handler(onFulfilled, onRejected, resolve, reject))
1717
})
1818
}

test/extensions-tests.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,4 +372,20 @@ describe('extensions', function () {
372372
})
373373
})
374374
})
375+
376+
describe('inheritance', function () {
377+
it('allows its prototype methods to act upon foreign constructors', function () {
378+
function Awesome(fn) {
379+
if (!(this instanceof Awesome)) return new Awesome(fn)
380+
Promise.call(this, fn)
381+
}
382+
Awesome.prototype = Object.create(Promise.prototype)
383+
Awesome.prototype.constructor = Awesome
384+
385+
var awesome = new Awesome(function () {})
386+
387+
assert(awesome.constructor === Awesome)
388+
assert(awesome.then(function () {}).constructor === Awesome)
389+
})
390+
})
375391
})

0 commit comments

Comments
 (0)