-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise.js
192 lines (162 loc) · 5.4 KB
/
promise.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
184
185
186
187
188
189
190
191
192
(function (window) {
var Promise = function (fn) {
var value = null;
var callbacks = [];
var state = 'pending'; // pengding, fulfilled, rejected
var promise = this;
// 注册then事件,供resolve后调用
promise.then = function (onFulfilled, onRejected) {
// 返回promise实现链式promise调用
return new Promise(function (resolve, reject) {
handle({
onFulfilled: onFulfilled || null,
onRejected: onRejected || null,
resolve: resolve,
reject: reject
})
})
}
promise.catch = function(onRejected) {
return promise.then(undefined, onRejected)
}
function handle (callback) {
// 状态变化前,事件推进队列里;状态一旦变化后不再变动,直接执行结果
if (state === 'pending') {
callbacks.push(callback);
} else {
var cb = state === 'fulfilled' ? callback.onFulfilled : callback.onRejected
// then方法没有传递任何参数的情况下,返回结果值
if (!cb) {
cb = state === 'fulfilled' ? callback.resolve : callback.reject;
cb(value)
} else {
try {
var ret = cb(value);
callback.resolve(ret);
} catch (e) {
callback.reject(e)
}
}
}
}
function resolve(newValue) {
if (state !== 'pending') return
// 假如resolve了一个promise的话(链式promise)
if (newValue && (typeof newValue === 'object' || typeof newValue === 'function')) {
var then = newValue.then;
if (typeof then === 'function') {
// 调用第二个promise中的then,递归直到不是一个promise值为止
then.call(newValue, resolve, reject);
return
}
}
value = newValue;
state = 'fulfilled';
execute()
}
function reject(reason) {
if (state !== 'pending') return
state = 'rejected'
value = reason
execute()
}
function execute() {
// 使用setTimeOut保证resolve一定在then事件注册后执行
setTimeout(() => {
callbacks.forEach(function (callback) {
handle(callback);
})
}, 0);
}
fn(resolve, reject);
}
Promise.all = function (promises) {
if (!Array.isArray(promises)) {
throw new TypeError('请传入promise数组')
}
return new Promise(function (resolve, reject) {
var result = [];
var count = promises.length
function reslover (index) {
return function(value) {
resloveAll(index, value)
}
}
function rejecter (reason) {
reject(reason)
}
function resloveAll (index, value) {
result[index] = value
// 等待全部promise执行完才执行resolve一个数组
if (--count === 0) {
resolve(result)
}
}
promises.forEach(function (promise, index) {
promise.then(reslover(index), rejecter)
})
})
}
Promise.race = function (promises) {
if (!Array.isArray(promises)) {
throw new TypeError('请传入promise数组')
}
return new Promise(function (resolve, reject) {
function reslover (value) {
resolve(value)
}
function rejecter (reason) {
reject(reason)
}
promises.forEach(function (promise, index) {
promise.then(reslover, rejecter)
})
})
}
window.Promise = Promise
})(window)
/******************************************实例 */
function test(i) {
return new Promise(function (resolve) {
setTimeout(() => {
resolve(i);
}, 1000);
})
}
function test2(i) {
return new Promise(function (resolve, reject) {
setTimeout(() => {
if (i % 2) {
resolve(i);
} else {
reject(i);
}
}, 2000);
})
}
test(1).then(test2).then(function (something) {
console.log('case1: success!' + something);
}).catch(function (something) {
console.log('case1: failed!' + something);
})
Promise.all([test(2), test2(4)]).then(function (something) {
console.log('case2: success!' + something);
}).catch(function (something) {
console.log('case2: failed!' + something)
})
Promise.race([test(3), test2(5)]).then(function (something) {
console.log('case3: success!' + something);
}).catch(function (something) {
console.log('case3: failed!' + something)
})
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('success1')
reject('error')
resolve('success2')
},0)
})
promise
.then((res) => {
console.log('then: ', res)
})