Skip to content

Commit

Permalink
Version 8: Added finally polyfill, add module and removed Promise file
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorhakes committed May 5, 2018
1 parent b94f8b9 commit f13a09f
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 292 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# Changelog

### 8.0.0

* Polyfill default promise with `finally` if it doesn't exist if you use polyfill.js
* If using `require` with webpack 2+, you not need to do

```js
var Promise = require('promise-polyfill').default;
```

instead of

```js
var Promise = require('promise-polyfill');
```

* Removed files /dist/promise.js and /dist/promise.min.js. These files were not used unless you downloaded them directly from the repo. They were not used by ES6 modules, CommonJS or polyfill.js
The file lead to issues because they overrode the global Promise by default.

### 7.1.2

* Fixed bug in Node JS Promise polyfill
Expand Down
26 changes: 19 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ bower install promise-polyfill

### CDN Polyfill Use

This will set a global Promise object if the browser doesn't already have `window.Promise`.

```html
<script src="https://cdn.jsdelivr.net/npm/promise-polyfill@7/dist/polyfill.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"></script>
```

## Downloads
Expand All @@ -43,10 +45,26 @@ bower install promise-polyfill

## Simple use

If you would like to add a global Promise object (Node or Browser) if native Promise doesn't exist (polyfill Promise). Use the method below. This is useful it you are building a website and want to support older browsers.
Javascript library authors should _NOT_ use this method.

```js
import 'promise-polyfill/src/polyfill';
```

If you would like to not affect the global environment (sometimes known as a [ponyfill](ponyfill.com)), you can import the base module. This is nice for library authors or people working in environment where you don't want
to affect the global environment.

```js
import Promise from 'promise-polyfill';
```

If using `require` with Webpack 2+ (rare), you need to specify the default import

```js
var Promise = require('promise-polyfill').default;
```

then you can use like normal Promises

```js
Expand All @@ -65,12 +83,6 @@ prom.then(function(result) {
});
```

If you would like to just polyfill, only if native doesn't exist.

```js
import 'promise-polyfill/src/polyfill';
```

## Performance

By default promise-polyfill uses `setImmediate`, but falls back to `setTimeout`
Expand Down
34 changes: 19 additions & 15 deletions dist/polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@
(factory());
}(this, (function () { 'use strict';

var promiseFinally = function (callback) {
var constructor = this.constructor;
return this.then(
function(value) {
return constructor.resolve(callback()).then(function() {
return value;
});
},
function(reason) {
return constructor.resolve(callback()).then(function() {
return constructor.reject(reason);
});
}
);
};

// Store setTimeout reference so promise-polyfill will be unaffected by
// other code modifying setTimeout (like sinon.useFakeTimers())
var setTimeoutFunc = setTimeout;
Expand Down Expand Up @@ -149,21 +165,7 @@ Promise.prototype.then = function(onFulfilled, onRejected) {
return prom;
};

Promise.prototype['finally'] = function(callback) {
var constructor = this.constructor;
return this.then(
function(value) {
return constructor.resolve(callback()).then(function() {
return value;
});
},
function(reason) {
return constructor.resolve(callback()).then(function() {
return constructor.reject(reason);
});
}
);
};
Promise.prototype['finally'] = promiseFinally;

Promise.all = function(arr) {
return new Promise(function(resolve, reject) {
Expand Down Expand Up @@ -261,6 +263,8 @@ var globalNS = (function() {

if (!globalNS.Promise) {
globalNS.Promise = Promise;
} else if (!globalNS.Promise.prototype['finally']) {
globalNS.Promise.prototype['finally'] = promiseFinally;
}

})));
2 changes: 1 addition & 1 deletion dist/polyfill.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

248 changes: 0 additions & 248 deletions dist/promise.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/promise.min.js

This file was deleted.

Loading

0 comments on commit f13a09f

Please sign in to comment.