-
Notifications
You must be signed in to change notification settings - Fork 3
/
Promise.js
44 lines (43 loc) · 1.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
Promise.all = function(promises /* Array */) {
return new Promise((resolve, reject) => {
const arr = []
let count = 0;
const isPromise = v => typeof v === 'object' && typeof v.then === 'function' && typeof v.catch === 'function'
promises.forEach((p, index) => {
if(isPromise(p)) {
p.then(resolve,reject)
}else {
resolve(p)
}
count++
if(++count === promises.length) {
resolve(arr)
}
})
})
}
function all(promises) {
return new jPromise((resolve,reject)=>{
// 异步:并发 使用for循环迭代执行 和串行 就是回调,一个任务是下一个任务的前提
// 遍历数组,依次拿到结果
let arr = []
let index = 0;
const processData = (key,data) => {
arr[key] = data
if(++index === promises.length) {
resolve(arr)
}
}
for(let i=0;i<promises.length;i++) {
let result = promises[i];
if(isPromise(result)) {
result.then((data)=>{
processData(i,data)
},reject)
}else {
// 如果这个result不是promise那么直接返回它。
processData(i,result)
}
}
})
}