From 33dbcad07b762540f0f743f917be9bc829108bae Mon Sep 17 00:00:00 2001 From: taylorhakes Date: Wed, 20 Jul 2016 08:10:26 -0400 Subject: [PATCH] Exposed internal functions to make subclassing easier --- CHANGELOG.md | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 11 ++++-- package.json | 2 +- promise.js | 48 +++++++++++++++------------ 4 files changed, 129 insertions(+), 26 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..68fa4c7 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,94 @@ +# Changelog + +## 6.0.0 Deprecated `Promise._setImmediateFn` and `Promise._setUnhandledRejectionFn` +This allows subsclassing Promise much easier +- `Promise._setImmediateFn()` has been deprecated. Use `Promise._immediateFn = ;` instead. +- `Promise._setUnhandledRejectionFn()` has been deprecated. Use `Promise._unhandledRejectionFn = ` 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 diff --git a/README.md b/README.md index 2c9ede5..4830146 100644 --- a/README.md +++ b/README.md @@ -49,25 +49,30 @@ prom.then(function(result) { }); ``` +## Deprecations +- `Promise._setImmediateFn()` has been deprecated. Use `Promise._immediateFn = ;` instead. +- `Promise._setUnhandledRejectionFn()` has been deprecated. Use `Promise._unhandledRejectionFn = ` 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()`. If you would like to disable unhandled rejections. Use a noop like below. ```js -Promise._setUnhandledRejectionFn(function(rejectError) {}); +Promise._unhandledRejectionFn = function(rejectError) {}; ``` diff --git a/package.json b/package.json index 6f84dfc..c828441 100644 --- a/package.json +++ b/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": { diff --git a/promise.js b/promise.js index 785ecb6..77aa072 100644 --- a/promise.js +++ b/promise.js @@ -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 () { @@ -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); @@ -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); } }); } @@ -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) {