Skip to content

Commit

Permalink
[fixes #336] finally must not fail for non-callable callback
Browse files Browse the repository at this point in the history
  • Loading branch information
codeworrior committed Sep 7, 2018
1 parent 314e483 commit a770571
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/es6-promise/promise.js
Expand Up @@ -410,8 +410,12 @@ class Promise {
let promise = this;
let constructor = promise.constructor;

return promise.then(value => constructor.resolve(callback()).then(() => value),
reason => constructor.resolve(callback()).then(() => { throw reason; }));
if ( isFunction(callback) ) {
return promise.then(value => constructor.resolve(callback()).then(() => value),
reason => constructor.resolve(callback()).then(() => { throw reason; }));
}

return promise.then(callback, callback);
}
}

Expand Down
20 changes: 20 additions & 0 deletions test/extension-test.js
Expand Up @@ -1258,6 +1258,26 @@ describe('Promise.prototype.finally', function() {
done();
});
});

it("preserves the original fulfillment value even if a non-callable callback is given", function(done) {
var fulfillmentValue = 1;
var promise = Promise.resolve(fulfillmentValue);

promise['finally']().then(function(value) {
assert.equal(fulfillmentValue, value);
done();
});
});

it("preserves the original rejection reason even if a non-callable callback is given", function(done) {
var rejectionReason = new Error();
var promise = Promise.reject(rejectionReason);

promise['finally']().then(undefined, function(reason) {
assert.equal(rejectionReason, reason);
done();
});
});
});

describe("exception cases do propogate the failure", function(){
Expand Down

0 comments on commit a770571

Please sign in to comment.