-
Notifications
You must be signed in to change notification settings - Fork 45
/
vow.min.js
10 lines (10 loc) · 4.83 KB
/
vow.min.js
1
2
3
4
5
6
7
8
9
10
/**
* Vow
*
* Copyright (c) 2012-2013 Filatov Dmitry (dfilatov@yandex-team.ru)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* @version 0.2.1
*/(function(e){var t=function(e){this._res=e,this._isFulfilled=!!arguments.length,this._isRejected=!1,this._fulfilledCallbacks=[],this._rejectedCallbacks=[]};t.prototype={valueOf:function(){return this._res},isFulfilled:function(){return this._isFulfilled},isRejected:function(){return this._isRejected},isResolved:function(){return this._isFulfilled||this._isRejected},fulfill:function(e){if(this.isResolved())return;this._isFulfilled=!0,this._res=e,this._callCallbacks(this._fulfilledCallbacks),this._fulfilledCallbacks=this._rejectedCallbacks=r},reject:function(e){if(this.isResolved())return;this._isRejected=!0,this._res=e,this._callCallbacks(this._rejectedCallbacks),this._fulfilledCallbacks=this._rejectedCallbacks=r},then:function(e,n){var r=new t,i;return this._isRejected||(i={promise:r,fn:e},this._isFulfilled?this._callCallbacks([i]):this._fulfilledCallbacks.push(i)),this._isFulfilled||(i={promise:r,fn:n},this._isRejected?this._callCallbacks([i]):this._rejectedCallbacks.push(i)),r},fail:function(e){return this.then(r,e)},always:function(e){var t=this,n=function(){e(t)};return this.then(n,n)},spread:function(e,t){return this.then(function(t){return e.apply(this,t)},t)},done:function(){this.fail(u)},timeout:function(e){var n=new t,r=setTimeout(function(){n.reject(Error("timed out"))},e);return this.then(function(e){clearTimeout(r),n.fulfill(e)},function(e){clearTimeout(r),n.reject(e)}),n},sync:function(e){var t=this;e.then(function(e){t.fulfill(e)},function(e){t.reject(e)})},_callCallbacks:function(e){var t=e.length;if(!t)return;var r=this._res,s=this.isFulfilled();i(function(){var i=0,o,u,f;while(i<t){o=e[i++],u=o.promise,f=o.fn;if(a(f)){var l;try{l=f(r)}catch(c){u.reject(c);continue}n.isPromise(l)?function(e){l.then(function(t){e.fulfill(t)},function(t){e.reject(t)})}(u):u.fulfill(l)}else s?u.fulfill(r):u.reject(r)}})}};var n={promise:function(e){return arguments.length?this.isPromise(e)?e:new t(e):new t},when:function(e,t,n){return this.promise(e).then(t,n)},fail:function(e,t){return this.when(e,r,t)},always:function(e,t){return this.promise(e).always(t)},spread:function(e,t,n){return this.promise(e).spread(t,n)},done:function(e){this.isPromise(e)&&e.done()},isPromise:function(e){return e&&a(e.then)},valueOf:function(e){return this.isPromise(e)?e.valueOf():e},isFulfilled:function(e){return this.isPromise(e)?e.isFulfilled():!0},isRejected:function(e){return this.isPromise(e)?e.isRejected():!1},isResolved:function(e){return this.isPromise(e)?e.isResolved():!0},fulfill:function(e){return this.when(e,r,function(e){return e})},reject:function(e){return this.when(e,function(e){var n=new t;return n.reject(e),n})},resolve:function(e){return this.isPromise(e)?e:this.when(e)},forEach:function(e,t,n,r){var i=r?r.length:e.length,s=0;while(s<i)this.when(e[r?r[s]:s],t,n),++s},all:function(e){var r=new t,i=l(e),s=i?c(e):h(e),o=s.length,u=i?[]:{};if(!o)return r.fulfill(u),r;var a=o,f=function(){if(!--a){var t=0;while(t<o)u[s[t]]=n.valueOf(e[s[t++]]);r.fulfill(u)}},p=function(e){r.reject(e)};return this.forEach(e,f,p,s),r},allResolved:function(e){var n=new t,r=l(e),i=r?c(e):h(e),s=i.length,o=r?[]:{};if(!s)return n.fulfill(o),n;var u=function(){--s||n.fulfill(e)};return this.forEach(e,u,u,i),n},any:function(e){var n=new t,r=e.length;if(!r)return n.reject(Error()),n;var i=0,s,o=function(e){n.fulfill(e)},u=function(e){i||(s=e),++i===r&&n.reject(s)};return this.forEach(e,o,u),n},timeout:function(e,t){return this.promise(e).timeout(t)}},r,i=typeof process=="object"?process.nextTick:e.setImmediate?e.setImmediate:e.postMessage?function(){var t="__promise"+ +(new Date),n=function(e){e.data===t&&(e.stopPropagation&&e.stopPropagation(),o())};return e.addEventListener?e.addEventListener("message",n,!0):e.attachEvent("onmessage",n),function(n){s.push(n)===1&&e.postMessage(t,"*")}}():"onreadystatechange"in e.document.createElement("script")?function(){var t=function(){var t=document.createElement("script");t.onreadystatechange=function(){t.parentNode.removeChild(t),t=t.onreadystatechange=null,o()},(e.document.documentElement||e.document.body).appendChild(t)};return function(e){s.push(e)===1&&t()}}():function(e){setTimeout(e,0)},s=[],o=function(){var e=s,t=0,n=s.length;s=[];while(t<n)e[t++]()},u=function(e){i(function(){throw e})},a=function(e){return typeof e=="function"},f=Object.prototype.toString,l=Array.isArray||function(e){return f.call(e)==="[object Array]"},c=function(e){var t=[],n=0,r=e.length;while(n<r)t.push(n++);return t},h=Object.keys||function(e){var t=[];for(var n in e)e.hasOwnProperty(n)&&t.push(n);return t};typeof exports=="object"?module.exports=n:typeof define=="function"?define(function(e,t,r){r.exports=n}):e.Vow=n})(this);