native-promise-only
Advanced tools
Comparing version 0.3.0-a to 0.3.1-a
/*! Native Promise Only | ||
v0.3.0-a (c) Kyle Simpson | ||
v0.3.1-a (c) Kyle Simpson | ||
MIT License: http://getify.mit-license.org | ||
@@ -183,2 +183,4 @@ */ | ||
function extractChain(resolve,reject) { | ||
checkCapability(resolve,reject); | ||
this.chained.push({ | ||
@@ -197,11 +199,20 @@ resolve: resolve, | ||
function checkArray(arr) { | ||
function checkArray(Constructor,arr) { | ||
if (!Array.isArray(arr)) { | ||
return Promise.reject(TypeError("Expected array argument")); | ||
return Constructor.reject(TypeError("Expected array argument")); | ||
} | ||
} | ||
function iteratePromises(arr,resolver,rejecter) { | ||
function checkCapability(resolve,reject) { | ||
if (typeof resolve !== "function") { | ||
throw Error("Resolver is not a function"); | ||
} | ||
if (typeof reject !== "function") { | ||
throw Error("Rejecter is not a function"); | ||
} | ||
} | ||
function iteratePromises(Constructor,arr,resolver,rejecter) { | ||
arr.forEach(function item(v,idx) { | ||
Promise.resolve(v) | ||
Constructor.resolve(v) | ||
.then( | ||
@@ -224,3 +235,3 @@ resolver.bind(null,idx), | ||
var p = new Promise(extractChain.bind(this)); | ||
var p = new this.Constructor(extractChain.bind(this)); | ||
this.chained[this.chained.length-1].promise = p; | ||
@@ -243,2 +254,3 @@ | ||
var def = { | ||
Constructor: Promise, | ||
promise: this, | ||
@@ -267,9 +279,12 @@ state: 0, | ||
builtInProp(Promise,"resolve",function Promise$resolve(msg) { | ||
var Constructor = this; | ||
// spec mandated checks | ||
// note: best "isPromise" check that's practical for now | ||
if (typeof msg === "object" && msg instanceof Promise) { | ||
if (typeof msg === "object" && msg instanceof Constructor) { | ||
return msg; | ||
} | ||
return new Promise(function executor(resolve){ | ||
return new Constructor(function executor(resolve,reject){ | ||
checkCapability(resolve,reject); | ||
schedule(immediateResolve.bind(null,resolve,msg)); | ||
@@ -280,3 +295,4 @@ }); | ||
builtInProp(Promise,"reject",function Promise$reject(msg) { | ||
return new Promise(function executor(_,reject){ | ||
return new this(function executor(resolve,reject){ | ||
checkCapability(resolve,reject); | ||
reject(msg); | ||
@@ -287,13 +303,13 @@ }); | ||
builtInProp(Promise,"all",function Promise$all(arr) { | ||
var err; | ||
var err, Constructor = this; | ||
// spec mandated checks | ||
if ((err = checkArray(arr))) { | ||
if ((err = checkArray(Constructor,arr))) { | ||
return err; | ||
} | ||
if (arr.length === 0) { | ||
return Promise.resolve([]); | ||
return Constructor.resolve([]); | ||
} | ||
return new Promise(function executor(resolve,reject){ | ||
return new Constructor(function executor(resolve,reject){ | ||
function resolveCheck(idx,msg) { | ||
@@ -307,5 +323,7 @@ msgs[idx] = msg; | ||
checkCapability(resolve,reject); | ||
var msgs = [], count = 0, len = arr.length; | ||
iteratePromises(arr,resolveCheck,reject); | ||
iteratePromises(Constructor,arr,resolveCheck,reject); | ||
}); | ||
@@ -315,11 +333,13 @@ }); | ||
builtInProp(Promise,"race",function Promise$race(arr) { | ||
var err; | ||
var err, Constructor = this; | ||
// spec mandated checks | ||
if ((err = checkArray(arr))) { | ||
if ((err = checkArray(Constructor,arr))) { | ||
return err; | ||
} | ||
return new Promise(function executor(resolve,reject){ | ||
iteratePromises(arr,function resolver(idx,msg){ | ||
return new Constructor(function executor(resolve,reject){ | ||
checkCapability(resolve,reject); | ||
iteratePromises(Constructor,arr,function resolver(idx,msg){ | ||
immediateResolve(resolve,msg); | ||
@@ -326,0 +346,0 @@ },reject); |
/*! Native Promise Only | ||
v0.3.0-a (c) Kyle Simpson | ||
v0.3.1-a (c) Kyle Simpson | ||
MIT License: http://getify.mit-license.org | ||
*/ | ||
!function(e,t,n){t[e]=t[e]||n(),"undefined"!=typeof module&&module.exports?module.exports=t[e]:"function"==typeof define&&define.amd&&define(function(){return t[e]})}("Promise","undefined"!=typeof global?global:this,function(){"use strict";function e(e){g?(g=!1,e()):p?setImmediate(e):setTimeout(e,0)}function t(e){var t,n=typeof e;return null===e||"object"!==n&&"function"!==n||(t=e.then),"function"==typeof t?t:!1}function n(){var e,n,i,r,s;switch(this.state){case 0:return g=!1;case 1:s=this.success,this.failure.length=0;break;case 2:s=this.failure,this.success.length=0}for(;(e=s.shift())||e===!1;){r=this.chained.shift();try{e===!1?(g=!0,r.reject(this.msg)):(n=e===!0?this.msg:e(this.msg),g=!0,n===r.promise?r.reject(TypeError("Promise-chain cycle")):(i=t(n))?i.call(n,r.resolve,r.reject):r.resolve(n))}catch(c){g=!0,r.reject(c)}}}function i(r){var c,o,u;if(this.def){if(this.triggered)return g=!1;this.triggered=!0,o=this.def}else o=this;if(0!==o.state)return g=!1;u={def:o,triggered:!1};try{(c=t(r))?c.call(r,i.bind(u),s.bind(u)):(o.msg=r,o.state=1,e(n.bind(o)))}catch(f){s.call(u,f)}}function r(e){return this.triggered?void(g=!1):(this.triggered=!0,void i.call(this,e))}function s(t){var i;if(this.def){if(this.triggered)return g=!1;this.triggered=!0,i=this.def}else i=this;return 0!==i.state?g=!1:(i.msg=t,i.state=2,void e(n.bind(i)))}function c(e){return this.triggered?void(g=!1):(this.triggered=!0,void s.call(this,e))}function o(e,t){this.chained.push({resolve:e,reject:t})}function u(e,t){g=!0,e(t)}function f(e){return Array.isArray(e)?void 0:l.reject(TypeError("Expected array argument"))}function h(e,t,n){e.forEach(function(e,i){l.resolve(e).then(t.bind(null,i),n)})}function a(t,i){this.success.push("function"==typeof t?t:!0),this.failure.push("function"==typeof i?i:!1);var r=new l(o.bind(this));return this.chained[this.chained.length-1].promise=r,e(n.bind(this)),r}function d(e){return this.promise.then(void 0,e)}function l(e){if("function"!=typeof e)throw TypeError("Expected function argument");var t={promise:this,state:0,triggered:!1,success:[],failure:[],chained:[]};v(this,"then",a.bind(t)),v(this,"catch",d.bind(t));try{e(r.bind(t),c.bind(t))}catch(n){s.call(t,n)}}var g=!1,p="undefined"!=typeof setImmediate,v=Object.defineProperty?function(e,t,n,i){return Object.defineProperty(e,t,{value:n,writable:!0,configurable:i!==!1})}:function(e,t,n){return e[t]=n,e};return v(l,"prototype",{value:v({},"constructor",l)},!1),v(l,"resolve",function(t){return"object"==typeof t&&t instanceof l?t:new l(function(n){e(u.bind(null,n,t))})}),v(l,"reject",function(e){return new l(function(t,n){n(e)})}),v(l,"all",function(e){var t;return(t=f(e))?t:0===e.length?l.resolve([]):new l(function(t,n){function i(e,n){r[e]=n,s++,s===c&&u(t,r)}var r=[],s=0,c=e.length;h(e,i,n)})}),v(l,"race",function(e){var t;return(t=f(e))?t:new l(function(t,n){h(e,function(e,n){u(t,n)},n)})}),l}); | ||
!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(t){p?(p=!1,t()):v?setImmediate(t):setTimeout(t,0)}function e(t){var e,n=typeof t;return null===t||"object"!==n&&"function"!==n||(e=t.then),"function"==typeof e?e:!1}function n(){var t,n,i,r,o;switch(this.state){case 0:return p=!1;case 1:o=this.success,this.failure.length=0;break;case 2:o=this.failure,this.success.length=0}for(;(t=o.shift())||t===!1;){r=this.chained.shift();try{t===!1?(p=!0,r.reject(this.msg)):(n=t===!0?this.msg:t(this.msg),p=!0,n===r.promise?r.reject(TypeError("Promise-chain cycle")):(i=e(n))?i.call(n,r.resolve,r.reject):r.resolve(n))}catch(s){p=!0,r.reject(s)}}}function i(r){var s,c,u;if(this.def){if(this.triggered)return p=!1;this.triggered=!0,c=this.def}else c=this;if(0!==c.state)return p=!1;u={def:c,triggered:!1};try{(s=e(r))?s.call(r,i.bind(u),o.bind(u)):(c.msg=r,c.state=1,t(n.bind(c)))}catch(f){o.call(u,f)}}function r(t){return this.triggered?void(p=!1):(this.triggered=!0,void i.call(this,t))}function o(e){var i;if(this.def){if(this.triggered)return p=!1;this.triggered=!0,i=this.def}else i=this;return 0!==i.state?p=!1:(i.msg=e,i.state=2,void t(n.bind(i)))}function s(t){return this.triggered?void(p=!1):(this.triggered=!0,void o.call(this,t))}function c(t,e){h(t,e),this.chained.push({resolve:t,reject:e})}function u(t,e){p=!0,t(e)}function f(t,e){return Array.isArray(e)?void 0:t.reject(TypeError("Expected array argument"))}function h(t,e){if("function"!=typeof t)throw Error("Resolver is not a function");if("function"!=typeof e)throw Error("Rejecter is not a function")}function a(t,e,n,i){e.forEach(function(e,r){t.resolve(e).then(n.bind(null,r),i)})}function d(e,i){this.success.push("function"==typeof e?e:!0),this.failure.push("function"==typeof i?i:!1);var r=new this.Constructor(c.bind(this));return this.chained[this.chained.length-1].promise=r,t(n.bind(this)),r}function l(t){return this.promise.then(void 0,t)}function g(t){if("function"!=typeof t)throw TypeError("Expected function argument");var e={Constructor:g,promise:this,state:0,triggered:!1,success:[],failure:[],chained:[]};y(this,"then",d.bind(e)),y(this,"catch",l.bind(e));try{t(r.bind(e),s.bind(e))}catch(n){o.call(e,n)}}var p=!1,v="undefined"!=typeof setImmediate,y=Object.defineProperty?function(t,e,n,i){return Object.defineProperty(t,e,{value:n,writable:!0,configurable:i!==!1})}:function(t,e,n){return t[e]=n,t};return y(g,"prototype",{value:y({},"constructor",g)},!1),y(g,"resolve",function(e){var n=this;return"object"==typeof e&&e instanceof n?e:new n(function(n,i){h(n,i),t(u.bind(null,n,e))})}),y(g,"reject",function(t){return new this(function(e,n){h(e,n),n(t)})}),y(g,"all",function(t){var e,n=this;return(e=f(n,t))?e:0===t.length?n.resolve([]):new n(function(e,i){function r(t,n){o[t]=n,s++,s===c&&u(e,o)}h(e,i);var o=[],s=0,c=t.length;a(n,t,r,i)})}),y(g,"race",function(t){var e,n=this;return(e=f(n,t))?e:new n(function(e,i){h(e,i),a(n,t,function(t,n){u(e,n)},i)})}),g}); |
{ | ||
"name": "native-promise-only", | ||
"version": "0.3.0-a", | ||
"version": "0.3.1-a", | ||
"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
20015
352