stefanpenner / es6-promise

A polyfill for ES6-style Promises
MIT License
7.3k stars 593 forks source link

polyfill() does not polyfill Promise.prototype.finally() if the environment already has a Promise implementation. #330

Open shirakaba opened 6 years ago

shirakaba commented 6 years ago

In polyfill.js, observe line 30: https://github.com/stefanpenner/es6-promise/blob/master/lib/es6-promise/polyfill.js#L30

As I understand it, if a Promise implementation is found on the global object, then the polyfill will use the global object's Promise implementation as-is, not augmenting it with the stage 4 ES9/ES2018 feature proposal provided in this library, Promise.prototype.finally().

Is this intended behaviour, or a bug? Surely if I have polyfilled es6-promise, I should be getting at least all the advertised features of es6-promise?

YurySolovyov commented 6 years ago

Do you know if it works in -auto variant?

shirakaba commented 6 years ago

@YurySolovyov require('es6-promise/auto') is just an alias for require('es6-promise').polyfill(), so this issue affects both usages.

YurySolovyov commented 6 years ago

Thanks, good to know

radarfox commented 5 years ago

Following browser only polyfill setup works for browsers:

It also polyfills promises used in Fetch API and other promise based APIs.

import Promise from 'es6-promise';
if (!('Promise' in window)) {
  window.Promise = Promise;
} else if (!('finally' in window.Promise.prototype)) {
  window.Promise.prototype.finally = Promise.prototype.finally;
}
gitgrimbo commented 5 years ago

Does the finally method even exist on the ES6Promise object?

In IE 11, I see the following if I include https://cdnjs.cloudflare.com/ajax/libs/es6-promise/4.1.1/es6-promise.min.js in a script tag.

new ES6Promise(function(){}).finally === undefined

If I include https://cdn.jsdelivr.net/bluebird/latest/bluebird.core.min.js I see:

new Promise(function(){}).finally === function(t){return this._passThrough(t,0,l,l)}

So for my application at least, using bluebird seemed to provide a Promise implementation with a finally method.

And the bluebird approach also provides the finally method when I test on MS Edge.

Montana commented 5 years ago

Thank you!