-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTPromise.js
170 lines (155 loc) · 4.38 KB
/
TPromise.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* author tanjian917@gmail.com
* date 2018-12-29
* version v0.0.1
*/
(function(_global){
function TPromise(callback){
let _this = this;
this._cbs = [];
this._result = null;
this.status = 'pending';
function resolve(data){
_this._result = {
type: 0,
data
};
_this.status = 'resolved';
_this.__dispatchCb.call(_this);
}
function reject(data){
_this._result = {
type: 1,
data
};
_this.status = 'reject';
_this.__dispatchCb.call(_this);
}
setTimeout(function(){
try{
callback.apply(_this,[resolve,reject]);
}catch(err){
reject(err);
}
},0);
return this;
}
TPromise.prototype._TYPE_THEN = 0;
TPromise.prototype._TYPE_CATCH = 1;
TPromise.prototype._TYPE_FINALLY = 2;
TPromise.prototype.__dispatchCb = function(){
let {type,data} = this._result,sThen = (type==0),_cbs = this._cbs;
while(_cbs.length){
let cbItem = _cbs.splice(0,1)[0],statuType = cbItem.type;
if(sThen && statuType === this._TYPE_THEN){
// call then function
data = cbItem.cb(data);
this._result.data = data;
}else if(!sThen && statuType === this._TYPE_CATCH){
// call catch function
data = cbItem.cb(data);
this._result.data = data;
}else if(cbItem.type === this._TYPE_FINALLY){
// call finally function
cbItem.cb()
}
if(!sThen)sThen = (cbItem.type==1);
}
}
TPromise.prototype.then = function(resolve,reject){
if((typeof resolve) === 'function')this._cbs.push({
type: this._TYPE_THEN,
cb: resolve
});
if((typeof reject) === 'function')this.catch(reject);
if(this._result)this.__dispatchCb(this._TYPE_THEN);
return this;
}
TPromise.prototype.catch = function(callback){
if((typeof callback) === 'function')this._cbs.push({
type: this._TYPE_CATCH,
cb: callback
});
if(this._result)this.__dispatchCb(this._TYPE_CATCH);
return this;
}
TPromise.prototype.finally = function(callback){
if((typeof callback) === 'function')this._cbs.push({
type: this._TYPE_FINALLY,
cb: callback
});
if(this._result)this.__dispatchCb(this._TYPE_FINALLY);
return this;
};
TPromise.resolve = function(val){
return new TPromise((resolve,reject)=>{
resolve(val);
});
}
TPromise.reject = function(val){
return new TPromise((resolve,reject)=>{
reject(val);
});
}
TPromise.race = function(promises){
let filterPromises = [];
let formataPromise = function(promise){
if(!(promise instanceof TPromise) && !(promise instanceof Promise))return TPromise.resolve(promise);
return promise;
}
if(promises instanceof Array){
for(let i=0,len=promises.length;i<len;i++){
filterPromises.push(formataPromise(promises[i]));
}
}else filterPromises.push(formataPromise(promises));
let hasThen = true;
return new Promise((resolve,reject)=>{
let eIndex = filterPromises.length;
for(let i=0;i<eIndex;i++){
let promise = filterPromises[i];
promise.then(result=>{
if(hasThen){
hasThen = false;
resolve(result);
}
});
}
});
}
TPromise.all = function(promises){
let filterPromises = [];
let formataPromise = function(promise){
if(!(promise instanceof TPromise))return TPromise.resolve(promise);
return promise;
}
if(promises instanceof Array){
for(let i=0,len=promises.length;i<len;i++){
filterPromises.push(formataPromise(promises[i]));
}
}else filterPromises.push(formataPromise(promises));
return new TPromise((resolve,reject)=>{
let isResolve = true,results = [],eIndex = filterPromises.length;
let iteratorPromise = function(iIndex){
if(iIndex>=eIndex){
if(isResolve)resolve(results);
else reject(results);
return;
}
let promise = filterPromises[iIndex];
promise.then(result=>{
results.push(result);
return result;
}).catch(err=>{
results.push(err);
isResolve = false;
return err;
}).finally(()=>{
iIndex ++;
iteratorPromise(iIndex);
})
}
iteratorPromise(0);
});
}
_global.TPromise = TPromise;
})(global || window);