imacros-promise-polyfill
Version:
Lightweight promise polyfill for iMacros. A+ compliant
1 lines • 3.1 kB
JavaScript
function noop(){}function bind(e,n){return function(){e.apply(n,arguments)}}function Promise(e){if("object"!=typeof this)throw new TypeError("Promises must be constructed via new");if("function"!=typeof e)throw new TypeError("not a function");this._state=0,this._handled=!1,this._value=void 0,this._deferreds=[],doResolve(e,this)}function handle(e,n){for(;3===e._state;)e=e._value;return 0===e._state?void e._deferreds.push(n):(e._handled=!0,void asap(function(){var t=1===e._state?n.onFulfilled:n.onRejected;if(null===t)return void(1===e._state?resolve:reject)(n.promise,e._value);var o;try{o=t(e._value)}catch(r){return void reject(n.promise,r)}resolve(n.promise,o)}))}function resolve(e,n){try{if(n===e)throw new TypeError("A promise cannot be resolved with itself.");if(n&&("object"==typeof n||"function"==typeof n)){var t=n.then;if(n instanceof Promise)return e._state=3,e._value=n,void finale(e);if("function"==typeof t)return void doResolve(bind(t,n),e)}e._state=1,e._value=n,finale(e)}catch(o){reject(e,o)}}function reject(e,n){e._state=2,e._value=n,finale(e)}function finale(e){2===e._state&&0===e._deferreds.length&&setTimeout(function(){e._handled||onUnhandledRejection(e._value)},1);for(var n=0,t=e._deferreds.length;t>n;n++)handle(e,e._deferreds[n]);e._deferreds=null}function Handler(e,n,t){this.onFulfilled="function"==typeof e?e:null,this.onRejected="function"==typeof n?n:null,this.promise=t}function doResolve(e,n){var t=!1;try{e(function(e){t||(t=!0,resolve(n,e))},function(e){t||(t=!0,reject(n,e))})}catch(o){if(t)return;t=!0,reject(n,o)}}var hdw=Components.classes["@mozilla.org/appshell/appShellService;1"].getService(Components.interfaces.nsIAppShellService).hiddenDOMWindow,setTimeoutFunc=hdw.setTimeout,asap="function"==typeof setImmediate&&setImmediate||function(e){setTimeoutFunc(e,1)},onUnhandledRejection=function(e){hdw.console.warn("Possible Unhandled Promise Rejection:",e)},isArray=Array.isArray||function(e){return"[object Array]"===Object.prototype.toString.call(e)};Promise.prototype["catch"]=function(e){return this.then(null,e)},Promise.prototype.then=function(e,n){var t=new Promise(noop);return handle(this,new Handler(e,n,t)),t},Promise.all=function(){var e=Array.prototype.slice.call(1===arguments.length&&isArray(arguments[0])?arguments[0]:arguments);return new Promise(function(n,t){function o(i,s){try{if(s&&("object"==typeof s||"function"==typeof s)){var c=s.then;if("function"==typeof c)return void c.call(s,function(e){o(i,e)},t)}e[i]=s,0===--r&&n(e)}catch(u){t(u)}}if(0===e.length)return n([]);for(var r=e.length,i=0;i<e.length;i++)o(i,e[i])})},Promise.resolve=function(e){return e&&"object"==typeof e&&e.constructor===Promise?e:new Promise(function(n){n(e)})},Promise.reject=function(e){return new Promise(function(n,t){t(e)})},Promise.race=function(e){return new Promise(function(n,t){for(var o=0,r=e.length;r>o;o++)e[o].then(n,t)})},Promise._setImmediateFn=function(e){asap=e},Promise._setUnhandledRejectionFn=function(e){onUnhandledRejection=e},"undefined"!=typeof module&&module.exports?module.exports=Promise:root.Promise||(root.Promise=Promise);