-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathPromise.race.js
59 lines (50 loc) · 1.5 KB
/
Promise.race.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
// Promise.race(iterable) 方法返回一个 promise,一旦迭代器中的某个promise解决或拒绝,返回的promise就会解决或拒绝
function promiseRace(promises) {
return new Promise(function(resolve, reject) {
// 返回一个promise
if(!Array.isArray(promises)) {
// 判断参数是否为数组
throw new TypeError(`argument must be a array`)
}
var promiseNum = promises.length; // 获取数组的长度
for(let i = 0; i < promiseNum; i++) {
Promise.resolve(promises[i]).then(value => {
return resolve(value)
}, error => {
return reject(error)
})
}
})
// return new Promise(function(resolve, reject) {
// if(!Array.isArray(promises)) {
// throw new TypeError(`argument must be a array`)
// }
// var promiseNum = promises.length; // 获取数组的长度
// for(let i = 0; i < promiseNum; i++) {
// Promise.resolve(promises[i]).then(value => {
// return resolve(value)
// }, error => {
// return reject(error)
// })
// }
// })
}
const promise1 = new Promise((resolve, reject) => {
setTimeout(resolve, 500, 'one');
});
const promise2 = new Promise((resolve, reject) => {
setTimeout(reject, 100, 'two');
});
Promise.race([promise1, promise2]).then((value) => {
console.log(value);
// Both resolve, but promise2 is faster
},(error) => {
console.log(error);
});
// expected output: "two"
promiseRace([promise1, promise2]).then((value) => {
console.log(value);
// Both resolve, but promise2 is faster
},(error) => {
console.log(error);
});