From 5d9290fc1e1c9dbfc0e60470f073e76865cd9aea Mon Sep 17 00:00:00 2001 From: Theodore Abshire Date: Mon, 9 Jan 2017 11:04:40 -0800 Subject: [PATCH] Fixed a bug with resolve in the promise polyfill. Now, when you pass a rejected or pending promise into Promise.resolve(), it won't count as resolved. Change-Id: Ib2ee6e12625753436bfeb0b418dd824df4bb422a --- lib/polyfill/promise.js | 6 ++++-- test/polyfill/promise_polyfill_unit.js | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/polyfill/promise.js b/lib/polyfill/promise.js index e42becc5f7..4bc5ec7a30 100644 --- a/lib/polyfill/promise.js +++ b/lib/polyfill/promise.js @@ -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; + }); }; diff --git a/test/polyfill/promise_polyfill_unit.js b/test/polyfill/promise_polyfill_unit.js index faad478038..a0c0418982 100644 --- a/test/polyfill/promise_polyfill_unit.js +++ b/test/polyfill/promise_polyfill_unit.js @@ -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;