-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpromise.js
128 lines (116 loc) · 3.67 KB
/
cpromise.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
function CPromise(executor) {
if (!(executor instanceof Function)) {
throw new TypeError("executor must be a function");
}
this.promiseStatus = "pending";
this.promiseValue = undefined;
this.resolveFunc = Function.prototype;
this.rejectFunc = Function.prototype;
var _this = this;
function change(status, value) {
if (_this.promiseStatus !== "pending") return;
_this.promiseStatus = status;
_this.promiseValue = value;
var timer = setTimeout(function () {
clearTimeout(timer);
timer = null;
var promiseStatus = _this.promiseStatus;
var promiseValue = _this.promiseValue;
promiseStatus === "fulfilled"
? _this.resolveFunc(promiseValue)
: _this.rejectFunc(promiseValue);
});
}
try {
executor(
function (value) {
change("fulfilled", value);
},
function (reason) {
change("rejected", reason);
}
);
} catch (e) {
change("rejected", e.message);
}
}
CPromise.resolve = function (value) {
return new CPromise(function (resolve, reject) {
resolve(value);
});
};
CPromise.reject = function (reason) {
return new CPromise(function (resolve, reject) {
reject(reason);
});
};
CPromise.prototype.then = function (resolveFunc, rejectFunc) {
if (!(resolveFunc instanceof Function)) {
resolveFunc = function (value) {
return CPromise.resolve(value);
};
}
if (!(rejectFunc instanceof Function)) {
rejectFunc = function (reason) {
return CPromise.reject(reason);
};
}
var _this = this;
return new CPromise(function (resolve, reject) {
_this.resolveFunc = function (value) {
try {
var res = resolveFunc(value);
res instanceof CPromise ? res.then(resolve, reject) : resolve(res);
} catch (e) {
reject(e.message);
}
};
_this.rejectFunc = function (reason) {
try {
var res = rejectFunc(reason);
res instanceof CPromise ? res.then(resolve, reject) : resolve(res);
} catch (e) {
reject(e.message);
}
};
});
};
CPromise.prototype.catch = function (rejectFunc) {
return this.then(null, rejectFunc);
};
CPromise.all = function (promiseArr) {
var index = 0;
var values = [];
return new CPromise(function (resolve, reject) {
for (var i = 0; i < promiseArr.length; i++) {
(function (i) {
var item = promiseArr[i];
item instanceof CPromise ? null : (item = CPromise.resolve(item));
item.then(function (value) {
index++;
values[i] = value;
if (index === promiseArr.length) {
resolve(values);
}
}).catch(function (reason) {
reject(reason);
});
})(i);
}
});
};
(function (global) {
// global=>window
// factory=>回调函数 function (window, noGlobal) {...}
"use strict"; // 严格模式
if (typeof module === "object" && typeof module.exports === "object") {
// 此条件成立说明当前运行代码的环境支持CommonJS规范
// 浏览器端不支持,NODE端是是支持的
module.exports = {
CPromise,
};
} else {
// 浏览器端运行
global.CPromise = CPromise;
}
})(typeof window !== "undefined" ? window : this);