forked from stackp/promisejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise.min.js
6 lines (6 loc) · 2.39 KB
/
promise.min.js
1
2
3
4
5
6
/*
* Copyright 2012-2013 (c) Pierre Duquesne <stackp@online.fr>
* Licensed under the New BSD License.
* https://github.com/stackp/promisejs
*/
!function(t){function e(){this._callbacks=[]}function n(t){function n(t){return function(){a+=1,i[t]=Array.prototype.slice.call(arguments),a===o&&r.done(i)}}var r=new e,i=[];if(!t||!t.length)return r.done(i),r;for(var a=0,o=t.length,s=0;o>s;s++)t[s].then(n(s));return r}function r(t,n){var i=new e;return 0===t.length?i.done.apply(i,n):t[0].apply(null,n).then(function(){t.splice(0,1),r(t,arguments).then(function(){i.done.apply(i,arguments)})}),i}function i(t){var e="";if("string"==typeof t)e=t;else{var n=encodeURIComponent,r=[];for(var i in t)t.hasOwnProperty(i)&&r.push(n(i)+"="+n(t[i]));e=r.join("&")}return e}function a(){var t;if(window.XMLHttpRequest)t=new XMLHttpRequest;else if(window.ActiveXObject)try{t=new ActiveXObject("Msxml2.XMLHTTP")}catch(e){t=new ActiveXObject("Microsoft.XMLHTTP")}return t}function o(t,n,r,o){function s(){c.abort(),f.done(u.ETIMEOUT,"",c)}var c,l,f=new e;r=r||{},o=o||{};try{c=a()}catch(h){return f.done(u.ENOXHR,""),f}l=i(r),"GET"===t&&l&&(n+="?"+l,l=null),c.open(t,n),c.withCredentials=u.withCredentials;var p="application/x-www-form-urlencoded";for(var d in o)o.hasOwnProperty(d)&&("content-type"===d.toLowerCase()?p=o[d]:c.setRequestHeader(d,o[d]));c.setRequestHeader("Content-type",p);var w=u.ajaxTimeout;if(w)var y=setTimeout(s,w);return c.onreadystatechange=function(){if(w&&clearTimeout(y),4===c.readyState){var t=!c.status||(c.status<200||c.status>=300)&&304!==c.status;f.done(t,c.responseText,c)}},c.send(l),f}function s(t){return function(e,n,r){return o(t,e,n,r)}}e.prototype.then=function(t,n){var r;return this._isdone?r=t.apply(n,this.result):(r=new e,this._callbacks.push(function(){var e=t.apply(n,arguments);e&&"function"==typeof e.then&&e.then(r.done,r)})),r},e.prototype.done=function(){this.result=arguments,this._isdone=!0;for(var t=0;t<this._callbacks.length;t++)this._callbacks[t].apply(null,arguments);this._callbacks=[]},this._withCredentials=!1,Object.defineProperty(this,"withCredentials",{get:function(){return this._withCredentials},set:function(t){this._withCredentials=t}});var u={Promise:e,join:n,chain:r,ajax:o,get:s("GET"),post:s("POST"),put:s("PUT"),del:s("DELETE"),withCredentials:withCredentials,ENOXHR:1,ETIMEOUT:2,ajaxTimeout:0};"function"==typeof define&&define.amd?define(function(){return u}):t.promise=u}(this);