Skip to content

Instantly share code, notes, and snippets.

@cdata
Created March 23, 2015 18:42
Show Gist options
  • Select an option

  • Save cdata/35f03dfebafd02fbaa0e to your computer and use it in GitHub Desktop.

Select an option

Save cdata/35f03dfebafd02fbaa0e to your computer and use it in GitHub Desktop.
!function(t,e){"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?module.exports=e():(t.Promise=t.Promise||e(),t.PromisePolyfill=e())}(this,function(t){return function(){function e(t){this.state=e.state.PENDING,this.promise=t,this.result=null,this.pendingFulfillmentHandlers=[],this.pendingRejectionHandlers=[]}function n(t){var n=this.__deferred__=new e(this);if(!t)throw new Error("Promise constructor takes a function argument");t(c(n.resolve,n),c(n.reject,n))}var i=function(t){return!(!t||"function"!=typeof t)},r=function(t){return!!(t instanceof n)},o=function(t){return!!t&&t.hasOwnProperty("length")&&!t.propertyIsEnumerable("length")},s=function(t){var e;return null===t?!1:(e=typeof t,!("object"!==e&&"function"!==e||!("then"in t)))},u=function(){return t.setImmediate?t.setImmediate:t.requestAnimationFrame?t.requestAnimationFrame:function(e){t.setTimeout(e,0)}}(),c=function(t,e){return t.bind?t.bind(e):function(){return t.apply(e,arguments)}};return e.state={PENDING:"pending",FULFILLED:"fulfilled",REJECTED:"rejected"},e.prototype.resolve=function(t){var n;if(this.isPending()){try{if(r(t))return t.then(c(this.resolve,this),c(this.reject,this));if(s(t)&&(n=t.then,i(n)))return c(function(){var e=!0;try{return n.call(t,c(function(t){e&&(e=!1,this.resolve(t))},this),c(function(t){e&&(e=!1,this.reject(t))},this))}catch(i){if(!e)return;e=!1,this.reject(i)}},this)()}catch(o){return this.reject(o)}this.result=t,this.state=e.state.FULFILLED,u(c(function(){for(var e;e=this.pendingFulfillmentHandlers.shift();)e(t)},this))}},e.prototype.reject=function(t){this.isPending()&&(this.result=t,this.state=e.state.REJECTED,u(c(function(){for(var e;e=this.pendingRejectionHandlers.shift();)e(t)},this)))},e.prototype.whenResolved=function(t){return this.isFulfilled()?u(c(function(){t(this.result)},this)):void(this.isPending()&&this.pendingFulfillmentHandlers.push(t))},e.prototype.whenRejected=function(t){return this.isRejected()?u(c(function(){t(this.result)},this)):void(this.isPending()&&this.pendingRejectionHandlers.push(t))},e.prototype.isPending=function(){return this.state===e.state.PENDING},e.prototype.isFulfilled=function(){return this.state===e.state.FULFILLED},e.prototype.isRejected=function(){return this.state===e.state.REJECTED},n.prototype.then=function(t,e){var r,o,s=new n(function(t,e){r=t,o=e});return this.__deferred__.whenResolved(function(e){var n;if(i(t))try{n=t(e)}catch(u){return o(u)}else n=e;return n===s?o(new TypeError):void r(n)}),this.__deferred__.whenRejected(function(t){var n;if(!i(e))return o(t);try{n=e(t)}catch(u){return o(u)}return n===s?o(new TypeError):void r(n)}),s},n.prototype["catch"]=function(t){return this.then(void 0,t)},n.all=function(t){var e,i,r=0,s=[],u=!1;return o(t)?(e=t.length,0===e?n.resolve(s):new n(function(o,c){for(i=0;e>i;++i)!function(t,i){n.cast(t).then(function(t){u||(s[i]=t,++r===e&&o(s))})["catch"](function(t){u=!0,c(t)})}(t[i],i)})):n.resolve(s)},n.cast=function(t){return t&&"object"==typeof t&&t.constructor===this?t:n.resolve(t)},n.resolve=function(t){return new n(function(e){e(t)})},n.reject=function(t){return new n(function(e,n){n(t)})},n.race=function(t){var e,i;return o(t)?(e=t.length,new n(function(r,o){for(i=0;e>i;++i)n.cast(t[i]).then(function(t){r(t)})["catch"](function(t){o(t)})})):n.resolve()},n}}("undefined"!=typeof global?global:this));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment