Skip to content

Commit

Permalink
Exposed internal functions to make subclassing easier
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorhakes committed Jul 20, 2016
1 parent c77b775 commit 33dbcad
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 26 deletions.
94 changes: 94 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,94 @@
# Changelog

## 6.0.0 Deprecated `Promise._setImmediateFn` and `Promise._setUnhandledRejectionFn`
This allows subsclassing Promise much easier
- `Promise._setImmediateFn(<immediateFn>)` has been deprecated. Use `Promise._immediateFn = <immediateFn>;` instead.
- `Promise._setUnhandledRejectionFn(<rejectionFn>)` has been deprecated. Use `Promise._unhandledRejectionFn = <rejectionFn>` instead.
These functions will be removed in the next major version.

### 5.2.1 setTimeout to 0
Fixed bug where setTimeout was set to 1 instead of 0 for async execution

### 5.2.0 Subclassing
Allowed Subclassing. [#27](https://github.com/taylorhakes/promise-polyfill/pull/27)

### 5.1.0 Fixed reliance on setTimeout
Changed possibly unhanded warnings to use asap function instead of setTimeout

## 5.0.0 Removed multiple params from Promise.all
Removed non standard functionality of passing multiple params to Promise.all. You must pass an array now. You must change this code
```js
Promise.all(prom1, prom2, prom3);
```
to this
```js
Promise.all([prom1, prom2, prom3]);
```

### 4.0.4 IE8 console.warn fix
IE8 does not have console, unless you open the developer tools. This fix checks to makes sure console.warn is defined before calling it.

### 4.0.3 Fix case in bower.json
bower.json had Promise.js instead of promise.js

### 4.0.2 promise.js case fix in package.json
Fixed promise.js in package.json. It was accidently published as Promise.js

## 4.0.1 Unhandled Rejections and Other Fixes
- Added unhandled rejection warnings to the console
- Removed Grunt, jasmine and other unused code
- Renamed Promise.js to lowercase promise.js in multiple places

### 3.0.1 Fixed shadowing issue on setTimeout
New version fixing this major bug https://github.com/taylorhakes/promise-polyfill/pull/17

## 3.0.0 Updated setTimeout to not be affected by test mocks
This is considered a breaking change because people may have been using this functionality. If you would like to keep version 2 functionality, set Promise._setImmediateFn on `promise-polyfill` like the code below.

```js
var Promise = require('promise-polyfill');
Promise._setImmedateFn(function(fn) {
setTimeout(fn, 1);
});
```

### 2.1.0 Promise._setImmedateFn
Removed dead code Promise.immedateFn and added Promise._setImmediateFn(fn);

### 2.0.2 Simplified Global detection
Simplified attaching to global object

### 2.0.1 Webworker bugfixes
Fixed Webworkers missing window object

## 2.0.0
**Changed the following line**
```
module.exports = root.Promise ? root.Promise : Promise;
```
to
```
module.exports = Promise;
```

This means the library will not use built-in Promise by default. This allows for more consistency.

You can easily add the functionality back.
```
var Promise = window.Promise || require('promise-polyfill');
```

**Added Promise.immediateFn to allow changing the setImmedate function**
```
Promise.immediateFn = window.setAsap;
```

### 1.1.4 Updated Promise to use correct global object in Browser and Node

### 1.1.3 Fixed browserify issue with `this`

### 1.1.2 Updated Promise.resolve to resolve with original Promise

### 1.1.0 Performance Improvements for Modern Browsers

## 1.0.1 Update README.md
11 changes: 8 additions & 3 deletions README.md
Expand Up @@ -49,25 +49,30 @@ prom.then(function(result) {
});
```

## Deprecations
- `Promise._setImmediateFn(<immediateFn>)` has been deprecated. Use `Promise._immediateFn = <immediateFn>;` instead.
- `Promise._setUnhandledRejectionFn(<rejectionFn>)` has been deprecated. Use `Promise._unhandledRejectionFn = <rejectionFn>` instead.
These functions will be removed in the next major version.

## Performance
By default promise-polyfill uses `setImmediate`, but falls back to `setTimeout` for executing asynchronously. If a browser does not support `setImmediate` (IE/Edge are the only browsers with setImmediate), you may see performance issues.
Use a `setImmediate` polyfill to fix this issue. [setAsap](https://github.com/taylorhakes/setAsap) or [setImmediate](https://github.com/YuzuJS/setImmediate) work well.

If you polyfill `window.setImmediate` or use `Promise._setImmediateFn(immedateFn)` it will be used instead of `window.setTimeout`
If you polyfill `window.setImmediate` or use `Promise._immediateFn = yourImmediateFn` it will be used instead of `window.setTimeout`
```
npm install setasap --save
```
```js
var Promise = require('promise-polyfill');
var setAsap = require('setasap');
Promise._setImmediateFn(setAsap);
Promise._immediateFn = setAsap;
```

## Unhandled Rejections
promise-polyfill will warn you about possibly unhandled rejections. It will show a console warning if a Promise is rejected, but no `.catch` is used. You can turn off this behavior by setting `Promise._setUnhandledRejectionFn(<rejectError>)`.
If you would like to disable unhandled rejections. Use a noop like below.
```js
Promise._setUnhandledRejectionFn(function(rejectError) {});
Promise._unhandledRejectionFn = function(rejectError) {};
```


Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "promise-polyfill",
"version": "5.2.1",
"version": "6.0.0",
"description": "Lightweight promise polyfill. A+ compliant",
"main": "promise.js",
"scripts": {
Expand Down
48 changes: 26 additions & 22 deletions promise.js
Expand Up @@ -4,21 +4,8 @@
// other code modifying setTimeout (like sinon.useFakeTimers())
var setTimeoutFunc = setTimeout;

function noop() {
}

// Use polyfill for setImmediate for performance gains
var asap = (typeof setImmediate === 'function' && setImmediate) ||
function (fn) {
setTimeoutFunc(fn, 0);
};

var onUnhandledRejection = function onUnhandledRejection(err) {
if (typeof console !== 'undefined' && console) {
console.warn('Possible Unhandled Promise Rejection:', err); // eslint-disable-line no-console
}
};

function noop() {}

// Polyfill for Function.prototype.bind
function bind(fn, thisArg) {
return function () {
Expand Down Expand Up @@ -46,7 +33,7 @@
return;
}
self._handled = true;
asap(function () {
Promise._immediateFn(function () {
var cb = self._state === 1 ? deferred.onFulfilled : deferred.onRejected;
if (cb === null) {
(self._state === 1 ? resolve : reject)(deferred.promise, self._value);
Expand Down Expand Up @@ -95,9 +82,9 @@

function finale(self) {
if (self._state === 2 && self._deferreds.length === 0) {
asap(function() {
Promise._immediateFn(function() {
if (!self._handled) {
onUnhandledRejection(self._value);
Promise._unhandledRejectionFn(self._value);
}
});
}
Expand Down Expand Up @@ -207,19 +194,36 @@
});
};

// Use polyfill for setImmediate for performance gains
Promise._immediateFn = (typeof setImmediate === 'function' && setImmediate) ||
function (fn) {
setTimeoutFunc(fn, 0);
};

Promise._unhandledRejectionFn = function _unhandledRejectionFn(err) {
if (typeof console !== 'undefined' && console) {
console.warn('Possible Unhandled Promise Rejection:', err); // eslint-disable-line no-console
}
};

/**
* Set the immediate function to execute callbacks
* @param fn {function} Function to execute
* @private
* @deprecated
*/
Promise._setImmediateFn = function _setImmediateFn(fn) {
asap = fn;
Promise._immediateFn = fn;
};

/**
* Change the function to execute on unhandled rejection
* @param {function} fn Function to execute on unhandled rejection
* @deprecated
*/
Promise._setUnhandledRejectionFn = function _setUnhandledRejectionFn(fn) {
onUnhandledRejection = fn;
Promise._unhandledRejectionFn = fn;
};

if (typeof module !== 'undefined' && module.exports) {
module.exports = Promise;
} else if (!root.Promise) {
Expand Down

0 comments on commit 33dbcad

Please sign in to comment.