-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
183 lines (175 loc) · 5.89 KB
/
test.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
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* Promise/A plus : https://promisesaplus.com/#requirements
*/
const PENDING = 'Pending';
const FULFILLED = 'Fulfilled';
const REJECTED = 'Rejected';
function PromiseZhou(resolver) {
//resolver一定得是方程
if (typeof resolver !== 'function') {
throw new TypeError('Promise resolver ' + resolver + ' is not a function');
}
//这里参考Promise的规范
if (!this instanceof PromiseZhou) {
return new PromiseZhou(resolver);
}
let self = this;
/**
* 发布订阅模式 : 详情得参考nodejs的事件模块 : http://nodejs.cn/api/events.html
*/
self.resolveCallbacks = [];//存放成功回调函数集合
self.rejectCallbacks = [];//存放失败时的回调函数集合
self.status = PENDING;//存放promise状态,三种。PENDING, FULFILLED, REJECTED
self.data = null//初始化resolved数据
self.reason = null//初始化reject数据
function resolve(value) {
setTimeout(function () {
/**
* settimeout 产生闭包
*/
if (self.status !== PENDING) {
return
}
self.status = FULFILLED;//改变promise状态
self.data = value;//存放待处理的数据
for (let i = 0; i < self.resolveCallbacks.length; i++) {
self.resolveCallbacks[i](value)
}
})
}
function reject(reason) {
setTimeout(function () {
if (self.status !== PENDING) {
return
}
self.status = REJECTED;//同上,改变promise状态
self.reason = reason;//保存错误信息
for (let i = 0; i < self.rejectCallbacks.length; i++) {
self.rejectCallbacks[i](reason)
}
})
}
/**
* resolver调用resolve 和 reject参数
*/
try {
resolver(resolve, reject);
} catch (error) {
reject(error)
}
}
function resolvePromise(promise, x, resolve, reject) {
var then
var thenCalledOrThrow = false
if (promise === x) {
return reject(new TypeError('Chaining cycle detected for promise!'))
}
if ((x !== null) && ((typeof x === 'object') || (typeof x === 'function'))) {
try {
then = x.then
if (typeof then === 'function') {
then.call(x, function rs(y) {
if (thenCalledOrThrow) return
thenCalledOrThrow = true
/**
* 得继续解析,解析一次,thenCalledOrThrow为 flag,利用了闭包
*/
return resolvePromise(promise, y, resolve, reject)
}, function rj(r) {
if (thenCalledOrThrow) return
thenCalledOrThrow = true
return reject(r)
})
} else {
return resolve(x)
}
} catch (e) {
if (thenCalledOrThrow) return
thenCalledOrThrow = true
return reject(e)
}
} else {
return resolve(x)
}
}
PromiseZhou.prototype.then = function then(OnResolved, OnRejected) {
//检查参数的正确性
OnResolved = (typeof OnResolved === 'function') ? OnResolved : function rs(tobeResolved) { return tobeResolved };
OnRejected = (typeof OnRejected === 'function') ? OnRejected : function rj(tobeRejected) { throw tobeRejected };
let self = this;
let promiseZhou;
if (self.status === FULFILLED) {
return promiseZhou = new PromiseZhou(function (resolve, reject) {
setTimeout(function () {
try {
let resolvedData = OnResolved(self.data);
resolvePromise(promiseZhou, resolvedData, resolve, reject)
} catch (error) {
return reject(error)
}
}, 0)
})
}
if (self.status === REJECTED) {
return promiseZhou = new PromiseZhou(function (resolve, reject) {
setTimeout(function () {
try {
let rejectData = onRejected(self.reason);
resolvePromise(promiseZhou, rejectData, resolve, reject)
} catch (error) {
return reject(error)
}
})
})
}
if (self.status === PENDING) {
/**
* function name need to be paid attention
*/
return promiseZhou = new PromiseZhou(function (resolve, reject) {
self.resolveCallbacks.push(function(value) {
try {
let resolvedValue = OnResolved(value);
resolvePromise(promiseZhou, resolvedValue, resolve, reject)
} catch (error) {
return reject(error)
}
})
self.rejectCallbacks.push(function(reason) {
try {
let rejectedValue = OnRejected(reason);
resolvePromise(promiseZhou, rejectedValue, resolve, reject)
} catch (error) {
return reject(error)
}
})
})
}
}
PromiseZhou.prototype.catch = function (Onrejected) {
return this.then(null, Onrejected)
}
PromiseZhou.resolve = function (value) {
var promise = new PromiseZhou(function (resolve, reject) {
resolvePromise(promise, value, resolve, reject)
})
return promise
}
PromiseZhou.reject = function (reason) {
return new PromiseZhou(function (resolve, reject) {
reject(reason)
})
}
PromiseZhou.deferred = function () {
var result = {};
result.promise = new PromiseZhou(function (resolve, reject) {
result.resolve = resolve;
result.reject = reject;
});
return result;
}
const pp = PromiseZhou.resolve('123213');
const pp1 = pp.then(function (res) {
console.log(res)
})
module.exports = PromiseZhou;