-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathspromise.js
2 lines (2 loc) · 6.33 KB
/
spromise.js
1
2
!function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;b="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,b.spromise=a()}}(function(){return function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b){!function(){"use strict";function c(a,b,c){return"function"==typeof a?a.apply(c,b||[]):a}function d(a){function b(){k--,k||i.resolve.call(j,h)}function d(a){return function(){h[a]=1===arguments.length?arguments[0]:arguments,b()}}function g(){var e,f,g;for(e=0,g=k;g>e;e++)f=a[e],f&&"function"==typeof f.then?f.then(d(e),i.reject):(h[e]=c(f),b())}a=a||[];var h=[],i=e.defer(),j=this,k=a.length;return a.length?(f(g),i):i.resolve(a)}var e=a("./promise"),f=a("./async");b.exports=d}()},{"./async":2,"./promise":3}],2:[function(a,b){!function(){"use strict";function a(a){c(a)}var c;a.delay=function(a,b,c){setTimeout(a.apply.bind(a,this,c||[]),b)},c="object"==typeof process&&"function"==typeof process.nextTick?process.nextTick:"function"==typeof setImmediate?setImmediate:function(a){setTimeout(a,0)},a.nextTick=c,b.exports=a}()},{}],3:[function(a,b){!function(){"use strict";function c(a,b){b=b||new d;var e=this;e.then=function(a,c){return b.then(a,c)},e.resolve=function(){return b.transition(i.resolved,arguments,this),e},e.reject=function(){return b.transition(i.rejected,arguments,this),e},e.promise={then:e.then,always:e.always,done:e.done,"catch":e.fail,fail:e.fail,notify:e.notify,state:e.state,constructor:c},e.promise.promise=e.promise,e.then.stateManager=b,a&&a.call(e,e.resolve,e.reject)}function d(a){this.state=i.pending,a&&a.state&&this.transition(a.state,a.value,a.context)}function e(a){this.promise=a.promise}function f(a){g.debug&&(console.error(a),a&&a.stack&&console.log(a.stack))}function g(a){return new c(a)}var h=a("./async"),i={pending:0,resolved:1,rejected:2,always:3,notify:4},j=["pending","resolved","rejected"];c.prototype.done=function(a){return this.then.stateManager.enqueue(i.resolved,a),this.promise},c.prototype["catch"]=c.prototype.fail=function(a){return this.then.stateManager.enqueue(i.rejected,a),this.promise},c.prototype["finally"]=c.prototype.always=function(a){return this.then.stateManager.enqueue(i.always,a),this.promise},c.prototype.notify=function(a){return this.then.stateManager.enqueue(i.notify,a),this.promise},c.prototype.state=function(){return j[this.then.stateManager.state]},c.prototype.isPending=function(){return this.then.stateManager.state===i.pending},c.prototype.isResolved=function(){return this.then.stateManager.state===i.resolved},c.prototype.isRejected=function(){return this.then.stateManager.state===i.resolved},c.prototype.delay=function(a){var b=this;return new c(function(c,d){b.then(function(){h.delay(c.bind(this),a,arguments)},d.bind(this))})},d.prototype.enqueue=function(a,b){function c(){d.state===a||i.always===a?b.apply(d.context,d.value):i.notify===a&&b.call(d.context,d.state,d.value)}this.state?k.asyncTask(c):(this.queue||(this.queue=[])).push(c);var d=this},d.prototype.transition=function(a,b,c){if(!this.state){this.state=a,this.context=c,this.value=b;var d=this.queue;d&&(this.queue=null,k.asyncQueue(d))}},d.prototype.then=function(a,b){var f=this;if(a=a&&"function"==typeof a?a:null,b=b&&"function"==typeof b?b:null,!a&&f.state===i.resolved||!b&&f.state===i.rejected)return new c(null,f);var g=new c;return f.enqueue(i.notify,function(c,f){var h=c===i.resolved?a||b:b||a;h&&(f=d.runHandler(c,f,this,g,h)),f!==!1&&new e({promise:g}).finalize(c,f,this)}),g},d.runHandler=function(a,b,c,d,e){try{b=e.apply(c,b)}catch(g){return f(g),d.reject.call(c,g),!1}return void 0===b?[]:[b]},e.prototype.finalize=function(a,b,d){var e,f,g=this,h=this.promise;if(b.length)if(e=b[0],e===h)f=h.reject.call(d,new TypeError("Resolution input must not be the promise being resolved"));else if(e&&e.constructor===c)f=e.notify(function(a,b){g.finalize(a,b,this)});else if(void 0!==e&&null!==e)switch(typeof e){case"object":case"function":f=this.runThenable(e,d)}f||(a===i.resolved?h.resolve.apply(d,b):h.reject.apply(d,b))},e.prototype.runThenable=function(a,b){var c=this,d=!1;try{var e=a.then;if("function"==typeof e)return e.call(a,function(){d||(d=!0,c.finalize(i.resolved,arguments,this))},function(){d||(d=!0,c.promise.reject.apply(this,arguments))}),!0}catch(f){return d||c.promise.reject.call(b,f),!0}return!1};var k={_asyncQueue:[],asyncTask:function(a){1===k._asyncQueue.push(a)&&h(k.taskRunner(k._asyncQueue))},asyncQueue:function(a){k.asyncTask(1===a.length?a[0]:k.taskRunner(a))},taskRunner:function(a){return function(){for(var b;b=a[0];)k._runTask(b),a.shift()}},_runTask:function(a){try{a()}catch(b){f(b)}}};g.prototype=c.prototype,g.defer=function(){return new c},g.reject=function(){return new c(null,new d({context:this,value:arguments,state:i.rejected}))},g.resolve=g.thenable=function(a){if(a){if(a.constructor===c)return a;if("function"==typeof a.then)return new c(a.then)}return new c(null,new d({context:this,value:arguments,state:i.resolved}))},g.delay=function(a){var b=Array.prototype.slice(arguments,1);return new c(function(c){h.delay(c.bind(this),a,b)})},g.states=i,g.debug=!1,b.exports=g}()},{"./async":2}],4:[function(a,b){!function(){"use strict";function c(a){return a?new d(function(b,c){function d(){h||(h=!0,b.apply(this,arguments))}function e(){h||(h=!0,c.apply(this,arguments))}var f,g,h=!1;for(f=0,g=a.length;g>f;f++)a[f].then(d,e)}):d.resolve()}var d=a("./promise");b.exports=c}()},{"./promise":3}],5:[function(a,b){!function(){"use strict";var c=a("./promise");c.async=a("./async"),c.when=a("./when"),c.all=a("./all"),c.race=a("./race"),b.exports=c}()},{"./all":1,"./async":2,"./promise":3,"./race":4,"./when":6}],6:[function(a,b){!function(){"use strict";function c(){var a=this,b=arguments;return new d(function(c,d){e.call(a,b).then(function(b){c.apply(a,b)},function(b){d.call(a,b)})})}var d=a("./promise"),e=a("./all");b.exports=c}()},{"./all":1,"./promise":3}]},{},[5])(5)});
//# sourceMappingURL=spromise.min.js.map