Skip to content

Commit 0d29e09

Browse files
Merge pull request then#35 from chrissrogers/inheritance-then-fix
Allows an inheriting constructor to return itself when calling .then
2 parents 3931a5e + b0fadff commit 0d29e09

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
@@ -250,4 +250,20 @@ describe('extensions', function () {
250250
})
251251
})
252252
})
253+
254+
describe('inheritance', function () {
255+
it('allows its prototype methods to act upon foreign constructors', function () {
256+
function Awesome(fn) {
257+
if (!(this instanceof Awesome)) return new Awesome(fn)
258+
Promise.call(this, fn)
259+
}
260+
Awesome.prototype = Object.create(Promise.prototype)
261+
Awesome.prototype.constructor = Awesome
262+
263+
var awesome = new Awesome(function () {})
264+
265+
assert(awesome.constructor === Awesome)
266+
assert(awesome.then(function () {}).constructor === Awesome)
267+
})
268+
})
253269
})

0 commit comments

Comments
 (0)