native-promise-only
Advanced tools
Comparing version 0.4.0-g to 0.4.0-h
/*! Native Promise Only | ||
v0.4.0-g (c) Kyle Simpson | ||
v0.4.0-h (c) Kyle Simpson | ||
MIT License: http://getify.mit-license.org | ||
@@ -10,3 +10,3 @@ */ | ||
if (typeof module !== "undefined" && module.exports) { module.exports = context[name]; } | ||
else if (typeof define === "function" && define.amd) { define(function(){ return context[name]; }); } | ||
else if (typeof define === "function" && define.amd) { define(function $AMD$(){ return context[name]; }); } | ||
})("Promise",typeof global !== "undefined" ? global : this,function DEF(){ | ||
@@ -96,3 +96,3 @@ /*jshint validthis:true */ | ||
function notify() { | ||
var self = this, cb, ret, _then, chain, i; | ||
var self = this, cb, chain, i; | ||
@@ -106,35 +106,36 @@ if (self.state === 0) { | ||
cb = (self.state === 1) ? chain.success : chain.failure; | ||
notifyIsolated(self,cb,chain); | ||
} | ||
self.chain.length = 0; | ||
} | ||
try { | ||
if (cb === false) { | ||
sync_schedule = true; | ||
chain.reject(self.msg); | ||
function notifyIsolated(self,cb,chain) { | ||
var ret, _then; | ||
try { | ||
if (cb === false) { | ||
sync_schedule = true; | ||
chain.reject(self.msg); | ||
} | ||
else { | ||
if (cb === true) ret = self.msg; | ||
else ret = cb.call(void 0,self.msg); | ||
sync_schedule = true; | ||
if (ret === chain.promise) { | ||
chain.reject(TypeError("Promise-chain cycle")); | ||
} | ||
else if ((_then = isThenable(ret))) { | ||
_then.call(ret,chain.resolve,chain.reject); | ||
} | ||
else { | ||
if (cb === true) ret = self.msg; | ||
else ret = cb.call(void 0,self.msg); | ||
sync_schedule = true; | ||
if (ret === chain.promise) { | ||
chain.reject(TypeError("Promise-chain cycle")); | ||
} | ||
else if ((_then = isThenable(ret))) { | ||
_then.call(ret,chain.resolve,chain.reject); | ||
} | ||
else { | ||
chain.resolve(ret); | ||
} | ||
chain.resolve(ret); | ||
} | ||
} | ||
catch (err) { | ||
sync_schedule = true; | ||
chain.reject(err); | ||
} | ||
} | ||
self.chain.length = 0; | ||
catch (err) { | ||
sync_schedule = true; | ||
chain.reject(err); | ||
} | ||
} | ||
function resolve(msg) { | ||
var _then, self = this, obj; | ||
function checkYourself(self) { | ||
if (self.def) { | ||
@@ -152,12 +153,17 @@ if (self.triggered) { | ||
obj = { | ||
def: self, | ||
triggered: false | ||
}; | ||
return self; | ||
} | ||
function resolve(msg) { | ||
var _then, def_wrapper, self = checkYourself(this); | ||
// self-check failed | ||
if (self === false) { return; } | ||
try { | ||
if ((_then = isThenable(msg))) { | ||
def_wrapper = new MakeDefWrapper(self); | ||
_then.call(msg, | ||
function $resolve$(){ resolve.apply(obj,arguments); }, | ||
function $reject$(){ reject.apply(obj,arguments); } | ||
function $resolve$(){ resolve.apply(def_wrapper,arguments); }, | ||
function $reject$(){ reject.apply(def_wrapper,arguments); } | ||
); | ||
@@ -172,3 +178,3 @@ } | ||
catch (err) { | ||
reject.call(obj,err); | ||
reject.call(def_wrapper || (new MakeDefWrapper(self)),err); | ||
} | ||
@@ -178,16 +184,7 @@ } | ||
function reject(msg) { | ||
var self = this; | ||
var self = checkYourself(this); | ||
if (self.def) { | ||
if (self.triggered) { | ||
return sync_schedule = false; | ||
} | ||
self.triggered = true; | ||
self = self.def; | ||
} | ||
// self-check failed | ||
if (self === false) { return; } | ||
if (self.state !== 0) { | ||
return sync_schedule = false; | ||
} | ||
self.msg = msg; | ||
@@ -232,2 +229,7 @@ self.state = 2; | ||
function MakeDefWrapper(self) { | ||
this.def = self; | ||
this.triggered = false; | ||
} | ||
function MakeDef(self) { | ||
@@ -251,3 +253,3 @@ this.promise = self; | ||
// both `def` and `Promise`. | ||
builtInProp(self,"then",function then(success,failure) { | ||
self.then = function then(success,failure) { | ||
var o = { | ||
@@ -268,7 +270,7 @@ success: typeof success === "function" ? success : true, | ||
return o.promise; | ||
}); | ||
}; | ||
// `catch` not allowed as identifier in older JS engines | ||
builtInProp(self,"catch",function $catch$(failure) { | ||
self["catch"] = function $catch$(failure) { | ||
return def.promise.then(void 0,failure); | ||
}); | ||
}; | ||
@@ -316,3 +318,3 @@ try { | ||
checkCapability(resolve,reject); | ||
schedule(function(){ | ||
schedule(function $immediateResolve$(){ | ||
immediateResolve(resolve,msg); | ||
@@ -319,0 +321,0 @@ }); |
/*! Native Promise Only | ||
v0.4.0-g (c) Kyle Simpson | ||
v0.4.0-h (c) Kyle Simpson | ||
MIT License: http://getify.mit-license.org | ||
*/ | ||
!function(t,e,n){e[t]=e[t]||n(),"undefined"!=typeof module&&module.exports?module.exports=e[t]:"function"==typeof define&&define.amd&&define(function(){return e[t]})}("Promise","undefined"!=typeof global?global:this,function(){"use strict";function t(){function t(t,e){this.fn=t,this.self=e,this.next=void 0}var e,n,r;return{add:function(i,o){r=new t(i,o),n?n.next=r:e=r,n=r,r=void 0},drain:function(){for(;e;)e.fn.call(e.self),e=e.next;d=n=e}}}function e(t,e){g?(g=!1,t.call(e)):(h.add(t,e),d||(d=p(h.drain)))}function n(t){var e,n=typeof t;return null===t||"object"!==n&&"function"!==n||(e=t.then),"function"==typeof e?e:!1}function r(){var t,e,r,i,o,c=this;if(0===c.state)return g=!1;for(o=0;o<c.chain.length;o++){i=c.chain[o],t=1===c.state?i.success:i.failure;try{t===!1?(g=!0,i.reject(c.msg)):(e=t===!0?c.msg:t.call(void 0,c.msg),g=!0,e===i.promise?i.reject(TypeError("Promise-chain cycle")):(r=n(e))?r.call(e,i.resolve,i.reject):i.resolve(e))}catch(f){g=!0,i.reject(f)}}c.chain.length=0}function i(t){var c,f,u=this;if(u.def){if(u.triggered)return g=!1;u.triggered=!0,u=u.def}if(0!==u.state)return g=!1;f={def:u,triggered:!1};try{(c=n(t))?c.call(t,function(){i.apply(f,arguments)},function(){o.apply(f,arguments)}):(u.msg=t,u.state=1,e(r,u))}catch(a){o.call(f,a)}}function o(t){var n=this;if(n.def){if(n.triggered)return g=!1;n.triggered=!0,n=n.def}return 0!==n.state?g=!1:(n.msg=t,n.state=2,void e(r,n))}function c(t,e){g=!0,t(e)}function f(t,e,n,r){for(var i=0;i<e.length;i++)t.resolve(e[i]).then(function(t){n(i,t)},r)}function u(t,e){return Array.isArray(e)?void 0:t.reject(TypeError("Not an array"))}function a(t,e){if("function"!=typeof t)throw TypeError("Not a function");if("function"!=typeof e)throw TypeError("Not a function")}function s(t){this.promise=t,this.state=0,this.triggered=!1,this.chain=[],this.msg=void 0}function l(t){if("function"!=typeof t)throw TypeError("Not a function");var n=this,c=new s(n);v(n,"then",function(t,n){var i={success:"function"==typeof t?t:!0,failure:"function"==typeof n?n:!1};return i.promise=new l(function(t,e){a(t,e),i.resolve=t,i.reject=e}),c.chain.push(i),e(r,c),i.promise}),v(n,"catch",function(t){return c.promise.then(void 0,t)});try{t.call(void 0,function(t){return c.triggered?void(g=!1):(c.triggered=!0,void i.call(c,t))},function(t){return c.triggered?void(g=!1):(c.triggered=!0,void o.call(c,t))})}catch(f){o.call(c,f)}}var d,h,g=!1,p="undefined"!=typeof setImmediate?function(t){return setImmediate(t)}:setTimeout,v=Object.defineProperty?function(t,e,n,r){return Object.defineProperty(t,e,{value:n,writable:!0,configurable:r!==!1})}:function(t,e,n){return t[e]=n,t};return h=t(),v(l,"prototype",{value:v({},"constructor",l)},!1),v(l,"resolve",function(t){var n=this;return"object"==typeof t&&t instanceof n?t:new n(function(n,r){a(n,r),e(function(){c(n,t)})})}),v(l,"reject",function(t){return new this(function(e,n){a(e,n),n(t)})}),v(l,"all",function(t){var e,n=this;return(e=u(n,t))?e:0===t.length?n.resolve([]):new n(function(e,r){a(e,r);var i=t.length,o=Array(i),u=0;f(n,t,function(t,n){o[t]=n,++u===i&&c(e,o)},r)})}),v(l,"race",function(t){var e,n=this;return(e=u(n,t))?e:new n(function(e,r){a(e,r),f(n,t,function(t,n){c(e,n)},r)})}),l}); | ||
!function(t,n,e){n[t]=n[t]||e(),"undefined"!=typeof module&&module.exports?module.exports=n[t]:"function"==typeof define&&define.amd&&define(function(){return n[t]})}("Promise","undefined"!=typeof global?global:this,function(){"use strict";function t(){function t(t,n){this.fn=t,this.self=n,this.next=void 0}var n,e,r;return{add:function(i,o){r=new t(i,o),e?e.next=r:n=r,e=r,r=void 0},drain:function(){for(;n;)n.fn.call(n.self),n=n.next;v=e=n}}}function n(t,n){y?(y=!1,t.call(n)):(g.add(t,n),v||(v=m(g.drain)))}function e(t){var n,e=typeof t;return null===t||"object"!==e&&"function"!==e||(n=t.then),"function"==typeof n?n:!1}function r(){var t,n,e,r=this;if(0===r.state)return y=!1;for(e=0;e<r.chain.length;e++)n=r.chain[e],t=1===r.state?n.success:n.failure,i(r,t,n);r.chain.length=0}function i(t,n,r){var i,o;try{n===!1?(y=!0,r.reject(t.msg)):(i=n===!0?t.msg:n.call(void 0,t.msg),y=!0,i===r.promise?r.reject(TypeError("Promise-chain cycle")):(o=e(i))?o.call(i,r.resolve,r.reject):r.resolve(i))}catch(c){y=!0,r.reject(c)}}function o(t){if(t.def){if(t.triggered)return y=!1;t.triggered=!0,t=t.def}return 0!==t.state?y=!1:t}function c(t){var i,u,a=o(this);if(a!==!1)try{(i=e(t))?(u=new d(a),i.call(t,function(){c.apply(u,arguments)},function(){f.apply(u,arguments)})):(a.msg=t,a.state=1,n(r,a))}catch(s){f.call(u||new d(a),s)}}function f(t){var e=o(this);e!==!1&&(e.msg=t,e.state=2,n(r,e))}function u(t,n){y=!0,t(n)}function a(t,n,e,r){for(var i=0;i<n.length;i++)t.resolve(n[i]).then(function(t){e(i,t)},r)}function s(t,n){return Array.isArray(n)?void 0:t.reject(TypeError("Not an array"))}function l(t,n){if("function"!=typeof t)throw TypeError("Not a function");if("function"!=typeof n)throw TypeError("Not a function")}function d(t){this.def=t,this.triggered=!1}function h(t){this.promise=t,this.state=0,this.triggered=!1,this.chain=[],this.msg=void 0}function p(t){if("function"!=typeof t)throw TypeError("Not a function");var e=this,i=new h(e);e.then=function(t,e){var o={success:"function"==typeof t?t:!0,failure:"function"==typeof e?e:!1};return o.promise=new p(function(t,n){l(t,n),o.resolve=t,o.reject=n}),i.chain.push(o),n(r,i),o.promise},e["catch"]=function(t){return i.promise.then(void 0,t)};try{t.call(void 0,function(t){return i.triggered?void(y=!1):(i.triggered=!0,void c.call(i,t))},function(t){return i.triggered?void(y=!1):(i.triggered=!0,void f.call(i,t))})}catch(o){f.call(i,o)}}var v,g,y=!1,m="undefined"!=typeof setImmediate?function(t){return setImmediate(t)}:setTimeout,w=Object.defineProperty?function(t,n,e,r){return Object.defineProperty(t,n,{value:e,writable:!0,configurable:r!==!1})}:function(t,n,e){return t[n]=e,t};return g=t(),w(p,"prototype",{value:w({},"constructor",p)},!1),w(p,"resolve",function(t){var e=this;return"object"==typeof t&&t instanceof e?t:new e(function(e,r){l(e,r),n(function(){u(e,t)})})}),w(p,"reject",function(t){return new this(function(n,e){l(n,e),e(t)})}),w(p,"all",function(t){var n,e=this;return(n=s(e,t))?n:0===t.length?e.resolve([]):new e(function(n,r){l(n,r);var i=t.length,o=Array(i),c=0;a(e,t,function(t,e){o[t]=e,++c===i&&u(n,o)},r)})}),w(p,"race",function(t){var n,e=this;return(n=s(e,t))?n:new e(function(n,r){l(n,r),a(e,t,function(t,e){u(n,e)},r)})}),p}); |
{ | ||
"name": "native-promise-only", | ||
"version": "0.4.0-g", | ||
"version": "0.4.0-h", | ||
"description": "Native Promise Only: A polyfill for native ES6 Promises **only**, nothing else.", | ||
@@ -5,0 +5,0 @@ "main": "./npo.js", |
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
20647
375