promise-polyfill
Advanced tools
Comparing version 1.1.0 to 1.1.2
{ | ||
"name": "promise-polyfill", | ||
"version": "1.1.0", | ||
"description": "Light weight promise polyfill. A+ compliant", | ||
"main": "Promise.js", | ||
"scripts": { | ||
"test": "./node_modules/.bin/promises-aplus-tests tests/adapter.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://taylorhakes@github.com/taylorhakes/promise-polyfill.git" | ||
}, | ||
"author": "Taylor Hakes", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/taylorhakes/promise-polyfill/issues" | ||
}, | ||
"homepage": "https://github.com/taylorhakes/promise-polyfill", | ||
"devDependencies": { | ||
"promises-aplus-tests": "^2.0.3" | ||
}, | ||
"dependencies": {} | ||
"name": "promise-polyfill", | ||
"version": "1.1.2", | ||
"description": "Light weight promise polyfill. A+ compliant", | ||
"main": "Promise.js", | ||
"scripts": { | ||
"test": "./node_modules/.bin/promises-aplus-tests tests/adapter.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://taylorhakes@github.com/taylorhakes/promise-polyfill.git" | ||
}, | ||
"author": "Taylor Hakes", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/taylorhakes/promise-polyfill/issues" | ||
}, | ||
"homepage": "https://github.com/taylorhakes/promise-polyfill", | ||
"devDependencies": { | ||
"promises-aplus-tests": "^2.0.3", | ||
"grunt": "^0.4.4", | ||
"grunt-contrib-uglify": "^0.4.0" | ||
}, | ||
"dependencies": {} | ||
} |
125
Promise.js
@@ -8,38 +8,6 @@ (function(global) { | ||
var asap = (function() { | ||
var callbacks, timeout, hiddenDiv; | ||
if(global.process && process.nextTick === 'function') { | ||
return process.nextTick; | ||
} else if(global.MutationObserver) { | ||
callbacks = []; | ||
hiddenDiv = document.createElement("div"); | ||
(new MutationObserver(executeCallbacks)).observe(hiddenDiv, { attributes: true }); | ||
return function (callback) { | ||
if( !callbacks.length) { | ||
hiddenDiv.setAttribute('yes', 'no'); | ||
} | ||
callbacks.push(callback); | ||
}; | ||
} else if(global.setImmediate) { | ||
return global.setImmediate; | ||
} else { | ||
callbacks = []; | ||
return function (callback){ | ||
callbacks.push(callback); | ||
if(!timeout) { | ||
timeout = setTimeout(executeCallbacks, 0); | ||
} | ||
}; | ||
} | ||
// Use polyfill for setImmediate for performance gains | ||
var asap = global.setImmediate || function(fn) { setTimeout(fn, 1); }; | ||
function executeCallbacks() { | ||
var cbList = callbacks; | ||
timeout = void 0; | ||
callbacks = []; | ||
for(var i = 0, len = cbList.length; i < len; i++) { | ||
cbList[i](); | ||
} | ||
} | ||
})(); | ||
// Polyfill for Function.prototype.bind | ||
function bind(fn, thisArg) { | ||
@@ -51,11 +19,9 @@ return function() { | ||
function isArray(value) { | ||
return Array.isArray ? Array.isArray(value) : Object.prototype.toString.call(value) === "[object Array]" | ||
} | ||
var isArray = Array.isArray || function(value) { return Object.prototype.toString.call(value) === "[object Array]" }; | ||
function Promise(fn) { | ||
if (typeof this !== 'object') throw new TypeError('Promises must be constructed via new') | ||
if (typeof fn !== 'function') throw new TypeError('not a function') | ||
this._state = null | ||
this._value = null | ||
if (typeof this !== 'object') throw new TypeError('Promises must be constructed via new'); | ||
if (typeof fn !== 'function') throw new TypeError('not a function'); | ||
this._state = null; | ||
this._value = null; | ||
this._deferreds = [] | ||
@@ -69,3 +35,3 @@ | ||
if (this._state === null) { | ||
this._deferreds.push(deferred) | ||
this._deferreds.push(deferred); | ||
return | ||
@@ -76,14 +42,14 @@ } | ||
if (cb === null) { | ||
(me._state ? deferred.resolve : deferred.reject)(me._value) | ||
return | ||
(me._state ? deferred.resolve : deferred.reject)(me._value); | ||
return; | ||
} | ||
var ret | ||
var ret; | ||
try { | ||
ret = cb(me._value) | ||
ret = cb(me._value); | ||
} | ||
catch (e) { | ||
deferred.reject(e) | ||
return | ||
deferred.reject(e); | ||
return; | ||
} | ||
deferred.resolve(ret) | ||
deferred.resolve(ret); | ||
}) | ||
@@ -94,33 +60,34 @@ } | ||
try { //Promise Resolution Procedure: https://github.com/promises-aplus/promises-spec#the-promise-resolution-procedure | ||
if (newValue === this) throw new TypeError('A promise cannot be resolved with itself.') | ||
if (newValue === this) throw new TypeError('A promise cannot be resolved with itself.'); | ||
if (newValue && (typeof newValue === 'object' || typeof newValue === 'function')) { | ||
var then = newValue.then | ||
var then = newValue.then; | ||
if (typeof then === 'function') { | ||
doResolve(bind(then, newValue), bind(resolve, this), bind(reject, this)) | ||
return | ||
doResolve(bind(then, newValue), bind(resolve, this), bind(reject, this)); | ||
return; | ||
} | ||
} | ||
this._state = true | ||
this._value = newValue | ||
finale.call(this) | ||
} catch (e) { reject.call(this, e) } | ||
this._state = true; | ||
this._value = newValue; | ||
finale.call(this); | ||
} catch (e) { reject.call(this, e); } | ||
} | ||
function reject(newValue) { | ||
this._state = false | ||
this._value = newValue | ||
finale.call(this) | ||
this._state = false; | ||
this._value = newValue; | ||
finale.call(this); | ||
} | ||
function finale() { | ||
for (var i = 0, len = this._deferreds.length; i < len; i++) | ||
handle.call(this, this._deferreds[i]) | ||
this._deferreds = null | ||
for (var i = 0, len = this._deferreds.length; i < len; i++) { | ||
handle.call(this, this._deferreds[i]); | ||
} | ||
this._deferreds = null; | ||
} | ||
function Handler(onFulfilled, onRejected, resolve, reject){ | ||
this.onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : null | ||
this.onRejected = typeof onRejected === 'function' ? onRejected : null | ||
this.resolve = resolve | ||
this.reject = reject | ||
this.onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : null; | ||
this.onRejected = typeof onRejected === 'function' ? onRejected : null; | ||
this.resolve = resolve; | ||
this.reject = reject; | ||
} | ||
@@ -138,14 +105,14 @@ | ||
fn(function (value) { | ||
if (done) return | ||
done = true | ||
onFulfilled(value) | ||
if (done) return; | ||
done = true; | ||
onFulfilled(value); | ||
}, function (reason) { | ||
if (done) return | ||
done = true | ||
onRejected(reason) | ||
if (done) return; | ||
done = true; | ||
onRejected(reason); | ||
}) | ||
} catch (ex) { | ||
if (done) return | ||
done = true | ||
onRejected(ex) | ||
if (done) return; | ||
done = true; | ||
onRejected(ex); | ||
} | ||
@@ -161,3 +128,3 @@ } | ||
return new Promise(function(resolve, reject) { | ||
handle.call(me, new Handler(onFulfilled, onRejected, resolve, reject)) | ||
handle.call(me, new Handler(onFulfilled, onRejected, resolve, reject)); | ||
}) | ||
@@ -214,2 +181,2 @@ }; | ||
}; | ||
})(this); | ||
})(this); |
@@ -1,6 +0,2 @@ | ||
(function(f){function g(a,b){return function(){a.apply(b,arguments)}}function r(a){return Array.isArray?Array.isArray(a):"[object Array]"===Object.prototype.toString.call(a)}function e(a){if("object"!==typeof this)throw new TypeError("Promises must be constructed via new");if("function"!==typeof a)throw new TypeError("not a function");this._value=this._state=null;this._deferreds=[];m(a,g(n,this),g(k,this))}function p(a){var b=this;null===this._state?this._deferreds.push(a):s(function(){var d=b._state? | ||
a.onFulfilled:a.onRejected;if(null===d)(b._state?a.resolve:a.reject)(b._value);else{var c;try{c=d(b._value)}catch(l){a.reject(l);return}a.resolve(c)}})}function n(a){try{if(a===this)throw new TypeError("A promise cannot be resolved with itself.");if(a&&("object"===typeof a||"function"===typeof a)){var b=a.then;if("function"===typeof b){m(g(b,a),g(n,this),g(k,this));return}}this._state=!0;this._value=a;q.call(this)}catch(d){k.call(this,d)}}function k(a){this._state=!1;this._value=a;q.call(this)}function q(){for(var a= | ||
0,b=this._deferreds.length;a<b;a++)p.call(this,this._deferreds[a]);this._deferreds=null}function t(a,b,d,c){this.onFulfilled="function"===typeof a?a:null;this.onRejected="function"===typeof b?b:null;this.resolve=d;this.reject=c}function m(a,b,d){var c=!1;try{a(function(a){c||(c=!0,b(a))},function(a){c||(c=!0,d(a))})}catch(l){c||(c=!0,d(l))}}"undefined"!==typeof module&&module.exports?module.exports=f.Promise?f.Promise:e:f.Promise||(f.Promise=e);var s=function(){function a(){var a=b;d=void 0;b=[]; | ||
for(var c=0,e=a.length;c<e;c++)a[c]()}var b,d,c;if(f.process&&"function"===process.nextTick)return process.nextTick;if(f.MutationObserver)return b=[],c=document.createElement("div"),(new MutationObserver(a)).observe(c,{attributes:!0}),function(a){b.length||c.setAttribute("yes","no");b.push(a)};if(f.setImmediate)return f.setImmediate;b=[];return function(c){b.push(c);d||(d=setTimeout(a,0))}}();e.prototype["catch"]=function(a){return this.then(null,a)};e.prototype.then=function(a,b){var d=this;return new e(function(c, | ||
e){p.call(d,new t(a,b,c,e))})};e.all=function(){var a=Array.prototype.slice.call(1===arguments.length&&r(arguments[0])?arguments[0]:arguments);return new e(function(b,d){function c(f,h){try{if(h&&("object"===typeof h||"function"===typeof h)){var g=h.then;if("function"===typeof g){g.call(h,function(a){c(f,a)},d);return}}a[f]=h;0===--e&&b(a)}catch(k){d(k)}}if(0===a.length)return b([]);for(var e=a.length,f=0;f<a.length;f++)c(f,a[f])})};e.resolve=function(a){return new e(function(b){b(a)})};e.reject= | ||
function(a){return new e(function(b,d){d(a)})};e.race=function(a){return new e(function(b,d){for(var c=0,e=a.length;c<e;c++)a[c].then(b,d)})}})(this); | ||
/*! promise-polyfill 1.1.2 */ | ||
!function(a){function b(a,b){return function(){a.apply(b,arguments)}}function c(a){if("object"!=typeof this)throw new TypeError("Promises must be constructed via new");if("function"!=typeof a)throw new TypeError("not a function");this._state=null,this._value=null,this._deferreds=[],i(a,b(e,this),b(f,this))}function d(a){var b=this;return null===this._state?void this._deferreds.push(a):void j(function(){var c=b._state?a.onFulfilled:a.onRejected;if(null===c)return void(b._state?a.resolve:a.reject)(b._value);var d;try{d=c(b._value)}catch(e){return void a.reject(e)}a.resolve(d)})}function e(a){try{if(a===this)throw new TypeError("A promise cannot be resolved with itself.");if(a&&("object"==typeof a||"function"==typeof a)){var c=a.then;if("function"==typeof c)return void i(b(c,a),b(e,this),b(f,this))}this._state=!0,this._value=a,g.call(this)}catch(d){f.call(this,d)}}function f(a){this._state=!1,this._value=a,g.call(this)}function g(){for(var a=0,b=this._deferreds.length;b>a;a++)d.call(this,this._deferreds[a]);this._deferreds=null}function h(a,b,c,d){this.onFulfilled="function"==typeof a?a:null,this.onRejected="function"==typeof b?b:null,this.resolve=c,this.reject=d}function i(a,b,c){var d=!1;try{a(function(a){d||(d=!0,b(a))},function(a){d||(d=!0,c(a))})}catch(e){if(d)return;d=!0,c(e)}}"undefined"!=typeof module&&module.exports?module.exports=a.Promise?a.Promise:c:a.Promise||(a.Promise=c);var j=a.setImmediate||function(a){setTimeout(a,1)},k=Array.isArray||function(a){return"[object Array]"===Object.prototype.toString.call(a)};c.prototype["catch"]=function(a){return this.then(null,a)},c.prototype.then=function(a,b){var e=this;return new c(function(c,f){d.call(e,new h(a,b,c,f))})},c.all=function(){var a=Array.prototype.slice.call(1===arguments.length&&k(arguments[0])?arguments[0]:arguments);return new c(function(b,c){function d(f,g){try{if(g&&("object"==typeof g||"function"==typeof g)){var h=g.then;if("function"==typeof h)return void h.call(g,function(a){d(f,a)},c)}a[f]=g,0===--e&&b(a)}catch(i){c(i)}}if(0===a.length)return b([]);for(var e=a.length,f=0;f<a.length;f++)d(f,a[f])})},c.resolve=function(a){return new c(function(b){b(a)})},c.reject=function(a){return new c(function(b,c){c(a)})},c.race=function(a){return new c(function(b,c){for(var d=0,e=a.length;e>d;d++)a[d].then(b,c)})}}(this); |
@@ -44,2 +44,5 @@ <a href="http://promises-aplus.github.com/promises-spec"> | ||
``` | ||
## Performance | ||
By default promise-polyfill uses `setImmediate`, but falls back to `setTimeout` for executing asynchronously. If a browser does not support `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. | ||
@@ -46,0 +49,0 @@ ## Testing |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10
56
11876
3
214