native-promise-only
Advanced tools
Comparing version 0.1.1-a to 0.2.0-a
/*! Native Promise Only | ||
v0.1.1-a (c) Kyle Simpson | ||
v0.2.0-a (c) Kyle Simpson | ||
MIT License: http://getify.mit-license.org | ||
@@ -14,2 +14,17 @@ */ | ||
var sync_schedule = false, | ||
builtInProp = Object.defineProperty ? | ||
function builtInProp(obj,name,val) { | ||
return Object.defineProperty(obj,name,{ | ||
value: val, | ||
writable: true, | ||
configurable: true | ||
}); | ||
} : | ||
function builtInProp(obj,name,val) { | ||
obj[name] = val; | ||
return obj; | ||
} | ||
; | ||
function schedule(fn) { | ||
@@ -174,2 +189,25 @@ if (sync_schedule) { | ||
// Note: only called within scheduled cycle, so "safe" to | ||
// skip cycle scheduling and resolve immediately | ||
function immediateResolve(resolve,msg) { | ||
sync_schedule = true; | ||
resolve(msg); | ||
} | ||
function checkArray(arr) { | ||
if (!Array.isArray(arr)) { | ||
return Promise.reject(TypeError("Expected array argument")); | ||
} | ||
} | ||
function iteratePromises(arr,resolver,rejecter) { | ||
arr.forEach(function item(v,idx) { | ||
Promise.resolve(v) | ||
.then( | ||
resolver.bind(null,idx), | ||
rejecter | ||
); | ||
}); | ||
} | ||
function then(success,failure) { | ||
@@ -210,4 +248,4 @@ // note: `this` check not necessary here. the following | ||
}; | ||
this.then = then.bind(def); | ||
this["catch"] = $catch$.bind(def); | ||
builtInProp(this,"then",then.bind(def)); | ||
builtInProp(this,"catch",$catch$.bind(def)); | ||
@@ -222,34 +260,8 @@ try { | ||
// Note: only called within scheduled cycle, so "safe" to | ||
// skip cycle scheduling and resolve immediately | ||
function immediateResolve(resolve,msg) { | ||
sync_schedule = true; | ||
resolve(msg); | ||
} | ||
builtInProp(Promise,"prototype",{ | ||
value: builtInProp({},"constructor",Promise) | ||
}); | ||
function checkArray(arr) { | ||
if (!Array.isArray(arr)) { | ||
return Promise.reject(TypeError("Expected array argument")); | ||
} | ||
} | ||
function checkPromiseConstructor(obj) { | ||
if (obj !== Promise) { | ||
throw TypeError("Expected Promise() constructor"); | ||
} | ||
} | ||
function iteratePromises(arr,resolver,rejecter) { | ||
arr.forEach(function item(v,idx) { | ||
Promise.resolve(v) | ||
.then( | ||
resolver.bind(null,idx), | ||
rejecter | ||
); | ||
}); | ||
} | ||
Promise.resolve = function Promise$resolve(msg) { | ||
builtInProp(Promise,"resolve",function Promise$resolve(msg) { | ||
// spec mandated checks | ||
checkPromiseConstructor(this); | ||
// note: best "isPromise" check that's practical for now | ||
@@ -259,21 +271,18 @@ if (typeof msg === "object" && msg instanceof Promise) { | ||
} | ||
return new Promise(function executor(resolve){ | ||
schedule(immediateResolve.bind(null,resolve,msg)); | ||
}); | ||
}; | ||
}); | ||
Promise.reject = function Promise$reject(msg) { | ||
// spec mandated check | ||
checkPromiseConstructor(this); | ||
builtInProp(Promise,"reject",function Promise$reject(msg) { | ||
return new Promise(function executor(_,reject){ | ||
reject(msg); | ||
}); | ||
}; | ||
}); | ||
Promise.all = function Promise$all(arr) { | ||
builtInProp(Promise,"all",function Promise$all(arr) { | ||
var err; | ||
// spec mandated checks | ||
checkPromiseConstructor(this); | ||
if ((err = checkArray(arr))) { | ||
@@ -299,9 +308,8 @@ return err; | ||
}); | ||
}; | ||
}); | ||
Promise.race = function Promise$race(arr) { | ||
builtInProp(Promise,"race",function Promise$race(arr) { | ||
var err; | ||
// spec mandated checks | ||
checkPromiseConstructor(this); | ||
if ((err = checkArray(arr))) { | ||
@@ -316,17 +324,5 @@ return err; | ||
}); | ||
}; | ||
}); | ||
if (Object.defineProperty) { | ||
Object.defineProperty(Promise,"prototype",{ | ||
value: Object.defineProperty({},"constructor",{ | ||
writable: true, | ||
configurable: true, | ||
value: Promise | ||
}) | ||
}); | ||
} | ||
var sync_schedule = false; | ||
return Promise; | ||
}); |
/*! Native Promise Only | ||
v0.1.1-a (c) Kyle Simpson | ||
v0.2.0-a (c) Kyle Simpson | ||
MIT License: http://getify.mit-license.org | ||
*/ | ||
!function(t,e,i){"undefined"!=typeof module&&module.exports?module.exports=i():"function"==typeof define&&define.amd?define(i):e[t]=e[t]||i(t,e)}("Promise",this,function(){"use strict";function t(t){p?(p=!1,t()):"undefined"!=typeof setImmediate?setImmediate(t):setTimeout(t,0)}function e(t){var e;return null===t||"object"!=typeof t&&"function"!=typeof t||(e=t.then),"function"==typeof e?e:!1}function i(){if(0===this.state)return p=!1;var t,i,n,r,s;for(1===this.state&&(s=this.success,this.failure.length=0),2===this.state&&(s=this.failure,this.success.length=0);(t=s.shift())||t===!1;){r=this.chained.shift();try{t===!1?(p=!0,r.reject(this.msg)):(i=t===!0?this.msg:t(this.msg),p=!0,i===r.promise?r.reject(TypeError("Promise-chain cycle")):(n=e(i))?n.call(i,r.resolve,r.reject):r.resolve(i))}catch(o){p=!0,r.reject(o)}}}function n(r){var o,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{(o=e(r))?o.call(r,n.bind(u),s.bind(u)):(c.msg=r,c.state=1,t(i.bind(c)))}catch(f){s.call(u,f)}}function r(t){return this.triggered?void(p=!1):(this.triggered=!0,void n.call(this,t))}function s(e){var n;if(this.def){if(this.triggered)return p=!1;this.triggered=!0,n=this.def}else n=this;return 0!==n.state?p=!1:(n.msg=e,n.state=2,void t(i.bind(n)))}function o(t){return this.triggered?void(p=!1):(this.triggered=!0,void s.call(this,t))}function c(t,e){this.chained.push({resolve:t,reject:e})}function u(e,n){this.success.push("function"==typeof e?e:!0),this.failure.push("function"==typeof n?n:!1);var r=new h(c.bind(this));return this.chained[this.chained.length-1].promise=r,t(i.bind(this)),r}function f(t){return this.promise.then(void 0,t)}function h(t){if("function"!=typeof t)throw TypeError("Expected function argument");var e={promise:this,state:0,triggered:!1,success:[],failure:[],chained:[]};this.then=u.bind(e),this["catch"]=f.bind(e);try{t(r.bind(e),o.bind(e))}catch(i){s.call(e,i)}}function d(t,e){p=!0,t(e)}function a(t){return Array.isArray(t)?void 0:h.reject(TypeError("Expected array argument"))}function l(t){if(t!==h)throw TypeError("Expected Promise() constructor")}function g(t,e,i){t.forEach(function(t,n){h.resolve(t).then(e.bind(null,n),i)})}h.resolve=function(e){return l(this),"object"==typeof e&&e instanceof h?e:new h(function(i){t(d.bind(null,i,e))})},h.reject=function(t){return l(this),new h(function(e,i){i(t)})},h.all=function(t){var e;return l(this),(e=a(t))?e:0===t.length?h.resolve([]):new h(function(e,i){function n(t,i){r[t]=i,s++,s===o&&d(e,r)}var r=[],s=0,o=t.length;g(t,n,i)})},h.race=function(t){var e;return l(this),(e=a(t))?e:new h(function(e,i){g(t,function(t,i){d(e,i)},i)})},Object.defineProperty&&Object.defineProperty(h,"prototype",{value:Object.defineProperty({},"constructor",{writable:!0,configurable:!0,value:h})});var p=!1;return h}); | ||
!function(t,e,i){"undefined"!=typeof module&&module.exports?module.exports=i():"function"==typeof define&&define.amd?define(i):e[t]=e[t]||i(t,e)}("Promise",this,function(){"use strict";function t(t){g?(g=!1,t()):"undefined"!=typeof setImmediate?setImmediate(t):setTimeout(t,0)}function e(t){var e;return null===t||"object"!=typeof t&&"function"!=typeof t||(e=t.then),"function"==typeof e?e:!1}function i(){if(0===this.state)return g=!1;var t,i,n,r,s;for(1===this.state&&(s=this.success,this.failure.length=0),2===this.state&&(s=this.failure,this.success.length=0);(t=s.shift())||t===!1;){r=this.chained.shift();try{t===!1?(g=!0,r.reject(this.msg)):(i=t===!0?this.msg:t(this.msg),g=!0,i===r.promise?r.reject(TypeError("Promise-chain cycle")):(n=e(i))?n.call(i,r.resolve,r.reject):r.resolve(i))}catch(o){g=!0,r.reject(o)}}}function n(r){var o,c,u;if(this.def){if(this.triggered)return g=!1;this.triggered=!0,c=this.def}else c=this;if(0!==c.state)return g=!1;u={def:c,triggered:!1};try{(o=e(r))?o.call(r,n.bind(u),s.bind(u)):(c.msg=r,c.state=1,t(i.bind(c)))}catch(f){s.call(u,f)}}function r(t){return this.triggered?void(g=!1):(this.triggered=!0,void n.call(this,t))}function s(e){var n;if(this.def){if(this.triggered)return g=!1;this.triggered=!0,n=this.def}else n=this;return 0!==n.state?g=!1:(n.msg=e,n.state=2,void t(i.bind(n)))}function o(t){return this.triggered?void(g=!1):(this.triggered=!0,void s.call(this,t))}function c(t,e){this.chained.push({resolve:t,reject:e})}function u(t,e){g=!0,t(e)}function f(t){return Array.isArray(t)?void 0:l.reject(TypeError("Expected array argument"))}function h(t,e,i){t.forEach(function(t,n){l.resolve(t).then(e.bind(null,n),i)})}function d(e,n){this.success.push("function"==typeof e?e:!0),this.failure.push("function"==typeof n?n:!1);var r=new l(c.bind(this));return this.chained[this.chained.length-1].promise=r,t(i.bind(this)),r}function a(t){return this.promise.then(void 0,t)}function l(t){if("function"!=typeof t)throw TypeError("Expected function argument");var e={promise:this,state:0,triggered:!1,success:[],failure:[],chained:[]};p(this,"then",d.bind(e)),p(this,"catch",a.bind(e));try{t(r.bind(e),o.bind(e))}catch(i){s.call(e,i)}}var g=!1,p=Object.defineProperty?function(t,e,i){return Object.defineProperty(t,e,{value:i,writable:!0,configurable:!0})}:function(t,e,i){return t[e]=i,t};return p(l,"prototype",{value:p({},"constructor",l)}),p(l,"resolve",function(e){return"object"==typeof e&&e instanceof l?e:new l(function(i){t(u.bind(null,i,e))})}),p(l,"reject",function(t){return new l(function(e,i){i(t)})}),p(l,"all",function(t){var e;return(e=f(t))?e:0===t.length?l.resolve([]):new l(function(e,i){function n(t,i){r[t]=i,s++,s===o&&u(e,r)}var r=[],s=0,o=t.length;h(t,n,i)})}),p(l,"race",function(t){var e;return(e=f(t))?e:new l(function(e,i){h(t,function(t,i){u(e,i)},i)})}),l}); |
{ | ||
"name": "native-promise-only", | ||
"version": "0.1.1-a", | ||
"version": "0.2.0-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
17998
334