手写promise.all 和 race
面试官:你能用 promise 实现一个 promise.all 和 race 么?
实现
js
Promise.MyAll = function (list) {
let count = 0;
let result = [];
return new Promise((resolve, reject) => {
for (let index = 0; index < list.length; index++) {
const p = list[index];
p.then((res) => {
result[index] = res;
count++;
count === list.length && resolve(result);
});
}
});
};
const p1 = new Promise((resolve, reject) => {
console.log(1);
resolve(1);
});
const p2 = new Promise((resolve, reject) => {
setTimeout(() => {
console.log(2);
resolve(2);
}, 1000);
});
const p3 = new Promise((resolve, reject) => {
console.log(3);
resolve(3);
});
Promise.MyAll([p1, p2, p3]).then((res) => {
console.log(res);
});
/**
1
3
2
[ 1, 2, 3 ]
*/
promise.race 和 all 同理,更简单
去掉 count
计数, 任意一个状态扭转成功后就返回
js
Promise.MyRace = function (list) {
return new Promise((resolve, reject) => {
for (let index = 0; index < list.length; index++) {
const p = list[index];
p.then((res) => {
return resolve(res);
});
}
});
};
const p1 = new Promise((resolve, reject) => {
console.log(1);
resolve(1);
});
const p2 = new Promise((resolve, reject) => {
setTimeout(() => {
console.log(2);
resolve(2);
}, 1000);
});
const p3 = new Promise((resolve, reject) => {
setTimeout(() => {
console.log(3);
resolve(3);
}, 2000);
});
Promise.MyRace([p1, p2, p3]).then((res) => {
console.log("接收数据", res);
});