-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPromise.js
173 lines (171 loc) · 6.24 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
// 在 reject 的时候会触发 onReject 不加 catch 也不会执行unhandleRejection 问题,加了catch执行了then中的onReject 就不会执行catch了,但是catch是为了处理在then中的error的
// 考虑 then 中传递的是不是函数时候默认值是上一个promise 中的值 let f = new Promise((res, rej) => res(1)).then(v=>4).then(5).then(console.log) 打印的是返回的 then 中函数 4 默认的 onFufill 是 默认透传上一个值的
class Promise {
constructor(fn) {
this.value = null;
this.status = 'pending';
this.resolveCallbacks = [];
this.rejectCallbacks = [];
let resolve = (val) => {
this.value = val;
this.status = 'fufilled';
setTimeout(() => {
this.resolveCallbacks.forEach(item => item(val))
}, 0)
}
let reject = (val) => {
this.value = val;
this.status = 'reject';
setTimeout(() => {
this.rejectCallbacks.forEach(item => item(val));
}, 0)
}
try {
fn(resolve, reject)
} catch(err) {
reject(err)
}
}
// promiseA+规范需要处理then是函数的普通对象为then中传递参数同时把resolve和reject传递进去
then(onFufill = val => val, onReject = val => val) {
// 处理thenable和then中非函数情况也会resolve但是会resolve只不过给的值是undefined
function dealThenableFunc(then, resolve, reject) {
then(function(val){
resolve(val)
}, function(err){
reject(err)
});
}
if(typeof onFufill !== 'function') {
onFufill = v => v
}
if(typeof onReject !== 'function') {
onReject = v => v
}
return new Promise((resolve, reject) => {
if(this.status !== 'pending') {
try {
let isFufilled = this.status !== 'reject';
let val = isFufilled ? onFufill(this.value) : onReject(this.value)
console.log(val, 'end')
if(val instanceof Promise) {
console.log('promise')
val.then(resolve, reject)
} else {
// 处理then函数但是还需要
if(typeof val === 'object') {
let then = val.then;
if(then && typeof then !== 'function') {
dealThenableFunc(then, resolve, reject)
return;
}
}
isFufilled ? resolve(val || this.value) : reject(val || this.value)
}
} catch(err) {
reject(err)
}
} else {
this.resolveCallbacks.push((val)=>{
try {
if(val instanceof Promise) {
val.then(resolve, reject)
} else {
if(typeof val === 'object') {
if(val.then instanceof Function) {
dealThenableFunc(val.then, resolve, reject)
return;
}
}
resolve(val)
}
} catch(err) {
reject(err)
}
})
this.rejectCallbacks.push((val)=>{
try {
if(val instanceof Promise) {
val.then(resolve, reject)
} else {
if(typeof val === 'object') {
if (val.then instanceof Function) {
dealThenableFunc(val.then, resolve, reject)
return;
}
}
reject(val)
}
} catch(err) {
reject(err)
}
})
}
})
}
catch(fn) {
return this.then(undefined, fn)
}
finally(cb) {
return this.then((v) => {
Promise.resolve(cb()).then(() => v)
}, err => {
Promise.resolve(cb()).then(() => throw err)
})
}
}
Promise.resolve = function(val) {
return new Promise((res, rej)=>{
res(val)
}).catch(err => err)
}
Promise.reject = function(val) {
return new Promise((res, rej)=>{
rej(val)
})
}
// 请记住all的实现好吗?再说一遍好不好?
Promise.all = function(pros) {
var length = props.length;
if(!length) return Promise.resolve(props);
let result = [];
let index = 0;
return new Promise(function(resolve, reject) {
for(let i = 0; i < props.length; i++) {
props[i].then(val => {
result[i] = val;
index++;
if(index === length) resolve(result)
}).catch(err => reject(err))
}
})
}
// 限制并行请求要求数组中的元素是返回promise函数, limit 代表并行限制数目
Promise.limit = function(arr, limit) {
let excuting = [];
let result = [];
let index = 0;
excute().then(()=>Promise.all(result))
function excute() {
if(index === arr.length) return Promise.resolve();
let newStart = Promise.resolve();
let p = Promise.resolve(arr[index++]());
result.push(p);
let e = p.then(val => excuting.splice(excuting.indexOf(e), 1));
excuting.push(e);
if(excuting.length >= limit) {
newStart = Promise.race(excuting)
}
return newStart.then(() => excute())
}
}
Promise.race = function(arr){
if(!arr.length) throw new Error('不可以使用空数组');
return new Promise(function(resolve, reject) {
for(let i = 0; i < arr.length; i++) {
arr[i].then(val => {
resolve(val)
}).catch(err => reject(err))
}
})
}