This repository has been archived on 2023-08-30. You can view files and clone it, but cannot push or open issues/pull-requests.
bytedo
/
wcui
Archived
1
0
Fork 0

更新request模块, 支持end不传参(返回Promise对象)

old
宇天 2017-11-08 13:44:26 +08:00
parent 99c95cf4a6
commit b13b8573b9
2 changed files with 606 additions and 645 deletions

File diff suppressed because it is too large Load Diff

View File

@ -5,233 +5,219 @@
* *
*/ */
"use strict"; 'use strict';
define(function(){ define(function() {
var _Promise = function(callback) {
this.callback = [];
var _this = this;
var _Promise = function(callback){ if (typeof this !== 'object')
this.callback = [] throw new TypeError('Promises must be constructed via new');
var _this = this
if(typeof this !== 'object') if (typeof callback !== 'function')
throw new TypeError('Promises must be constructed via new') throw new TypeError('Argument must be a function');
if(typeof callback !== 'function') callback(
throw new TypeError('Argument must be a function') function(val) {
_resolve(_this, val);
callback(function(val){ },
_resolve(_this, val) function(val) {
}, function(val){ _reject(_this, val);
_reject(_this, val) }
}) );
} };
var self = { var self = {
_state: 1, _state: 1,
_fired: 1, _fired: 1,
_val: 1, _val: 1,
callback: 1 callback: 1
} };
_Promise.prototype = { _Promise.prototype = {
constructor: _Promise, constructor: _Promise,
_state: 'pending', _state: 'pending',
_fired: false, _fired: false,
_fire: function(yes, no){ _fire: function(yes, no) {
if(this._state === 'rejected'){ if (this._state === 'rejected') {
if(typeof no === 'function') if (typeof no === 'function') no(this._val);
no(this._val) else throw this._val;
else } else {
throw this._val if (typeof yes === 'function') yes(this._val);
}else{
if(typeof yes === 'function')
yes(this._val)
} }
}, },
_then: function(yes, no){ _then: function(yes, no) {
if(this._fired){ if (this._fired) {
var _this = this var _this = this;
fireCallback(_this, function(){ fireCallback(_this, function() {
_this._fire(yes, no) _this._fire(yes, no);
}) });
}else{ } else {
this.callback.push({yes: yes, no: no}) this.callback.push({ yes: yes, no: no });
} }
}, },
then: function(yes, no){ then: function(yes, no) {
yes = typeof yes === 'function' ? yes : _yes yes = typeof yes === 'function' ? yes : _yes;
no = typeof no === 'function' ? no : _no no = typeof no === 'function' ? no : _no;
var _this = this var _this = this;
var next = new _Promise(function(resolve, reject){ var next = new _Promise(function(resolve, reject) {
_this._then(
_this._then(function(val){ function(val) {
try{ try {
val = yes(val) val = yes(val);
}catch(err){ } catch (err) {
return reject(err) return reject(err);
} }
}, function(val){ },
try{ function(val) {
val = no(val) try {
}catch(err){ val = no(val);
return reject(err) } catch (err) {
return reject(err);
} }
resolve(val) resolve(val);
}) }
}) );
for(var i in _this){ });
if(!self[i]) for (var i in _this) {
next[i] = _this[i] if (!self[i]) next[i] = _this[i];
} }
return next return next;
}, },
done: done, done: done,
catch: fail, catch: fail,
fail: fail fail: fail
};
_Promise.all = function(arr) {
return _some(false, arr);
};
_Promise.race = function(arr) {
return _some(true, arr);
};
_Promise.defer = defer;
// -----------------------------------------------------------
function _yes(val) {
return val;
} }
_Promise.all = function(arr){ function _no(err) {
return _some(false, arr) throw err;
} }
_Promise.race = function(arr){ function done(callback) {
return _some(true, arr) return this.then(callback, _no);
} }
_Promise.defer = defer function fail(callback) {
return this.then(_yes, callback);
// -----------------------------------------------------------
function _yes(val){
return val
} }
function _no(err){ function defer() {
throw err var obj = {};
obj.promise = new this(function(yes, no) {
obj.resolve = yes;
obj.reject = no;
});
return obj;
} }
function done(callback){
return this.then(callback, _no)
}
function fail(callback){
return this.then(_yes, callback)
}
function defer(){
var obj = {}
obj.promise = new this(function(yes, no){
obj.resolve = yes
obj.reject = no
})
return obj
}
//成功的回调 //成功的回调
function _resolve(obj, val){ function _resolve(obj, val) {
if(obj._state !== 'pending') if (obj._state !== 'pending') return;
return
if(val && typeof val.then === 'function'){ if (val && typeof val.then === 'function') {
var method = val instanceof _Promise ? '_then' : 'then' var method = val instanceof _Promise ? '_then' : 'then';
val[method](function(v){ val[method](
_transmit(obj, v, true) function(v) {
}, function(v){ _transmit(obj, v, true);
_transmit(obj, v, false) },
}) function(v) {
}else{ _transmit(obj, v, false);
_transmit(obj, val, true) }
);
} else {
_transmit(obj, val, true);
} }
} }
//失败的回调 //失败的回调
function _reject(obj, val){ function _reject(obj, val) {
if(obj._state !== 'pending') if (obj._state !== 'pending') return;
return
_transmit(obj, val, false) _transmit(obj, val, false);
} }
// 改变Promise的_fired值并保持用户传参触发所有回调 // 改变Promise的_fired值并保持用户传参触发所有回调
function _transmit(obj, val, isResolved){ function _transmit(obj, val, isResolved) {
obj._fired = true obj._fired = true;
obj._val = val obj._val = val;
obj._state = isResolved ? 'fulfilled' : 'rejected' obj._state = isResolved ? 'fulfilled' : 'rejected';
fireCallback(obj, function(){ fireCallback(obj, function() {
for(var i in obj.callback){ for (var i in obj.callback) {
obj._fire(obj.callback[i].yes, obj.callback[i].no) obj._fire(obj.callback[i].yes, obj.callback[i].no);
} }
}) });
} }
function fireCallback(obj, callback) {
var isAsync = false;
function fireCallback(obj, callback){ if (typeof obj.async === 'boolean') isAsync = obj.async;
var isAsync = false else isAsync = obj.async = true;
if(typeof obj.async === 'boolean') if (isAsync) setTimeout(callback, 0);
isAsync = obj.async else callback();
else
isAsync = obj.async = true
if(isAsync)
setTimeout(callback, 0)
else
callback()
} }
function _some(bool, iterable) {
iterable = Array.isArray(iterable) ? iterable : [];
function _some(bool, iterable){ var n = 0;
iterable = Array.isArray(iterable) ? iterable : [] var res = [];
var end = false;
var n = 0 return new _Promise(function(yes, no) {
var res = [] if (!iterable.length) no(res);
var end = false
return new _Promise(function(yes, no){ function loop(obj, idx) {
if(!iterable.length) obj.then(
no(res) function(val) {
if (!end) {
function loop(obj, idx){ res[idx] = val;
obj.then(function(val){ n++;
if(!end){ if (bool || n >= iterable.length) {
res[idx] = val yes(bool ? val : res);
n++ end = true;
if(bool || n >= iterable.length){ }
yes(bool ? val : res)
end = true
} }
},
function(val) {
end = true;
no(val);
} }
}, function(val){ );
end = true
no(val)
})
} }
for(var i = 0, len = iterable.length; i < len; i++){ for (var i = 0, len = iterable.length; i < len; i++) {
loop(iterable[i], i) loop(iterable[i], i);
} }
}) });
} }
// ---------------------------------------------------------------
// --------------------------------------------------------------- var nativePromise = window.Promise;
if (/native code/.test(nativePromise)) {
nativePromise.prototype.done = done;
var nativePromise = window.Promise nativePromise.prototype.fail = fail;
if(/native code/.test(nativePromise)){ if (!nativePromise.defer) nativePromise.defer = defer;
nativePromise.prototype.done = done
nativePromise.prototype.fail = fail
if(!nativePromise.defer)
nativePromise.defer = defer
} }
return window.Promise = nativePromise || _Promise return (window.Promise = nativePromise || _Promise);
});
})