function request1() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(1);
}, 100);
});
}
function request2() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(2);
}, 800);
});
}
function request3() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(3);
}, 500);
});
}
function request4() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(4);
}, 1000);
});
}
function schedular(max) {
// TODO
// code write here
}
const addReques = schedular(2);
addReques(request1).then((res) => {
console.log(res);
});
addReques(request2).then((res) => {
console.log(res);
});
addReques(request3).then((res) => {
console.log(res);
});
addReques(request4).then((res) => {
console.log(res);
});
如果是schedular传入一个请求数组写最大并发请求数我会,但是这种怎么实现和那个一样的效果?就是这里加入 max 是2,那么最多就只能同时执行2个请求,等到有一个请求完成后,才能进行后面的请求执行
简单实现了一下 可以作为参考