Skip to content

Commit

Permalink
Fixed a bug with resolve in the promise polyfill.
Browse files Browse the repository at this point in the history
Now, when you pass a rejected or pending promise into Promise.resolve(),
it won't count as resolved.

Change-Id: Ib2ee6e12625753436bfeb0b418dd824df4bb422a
  • Loading branch information
theodab committed Jan 9, 2017
1 parent b28f84c commit 292fd9a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/polyfill/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,10 @@ shaka.polyfill.Promise.uninstall = function() {
*/
shaka.polyfill.Promise.resolve = function(value) {
var p = new shaka.polyfill.Promise();
p.resolve_(value);
return p;
p.resolve_(undefined);
return p.then(function() {
return value;
});
};


Expand Down
18 changes: 18 additions & 0 deletions test/polyfill/promise_polyfill_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ describe('Promise polyfill', function() {
}, fail);
});

it('Promise.resolve takes on status of passed promise', function(done) {
var rejected = Promise.reject(1);
var pending = new Promise(function(resolve, reject) {});

var rejectedResult = 0;
Promise.resolve(rejected).then(fail, function(i) {
// This should be called with the value of 1, as it's rejected.
rejectedResult = i;
});
// This should never be called, as it's pending.
Promise.resolve(pending).then(fail);

setTimeout(function() {
expect(rejectedResult).toBe(1);
done();
}, 50);
});

it('Promise constructor arguments correctly resolve Promise',
function(done) {
var resolved = false;
Expand Down

0 comments on commit 292fd9a

Please sign in to comment.