当我对 API 进行大量调用时,我想要一种方法来限制我同时执行的调用量。
我发现 p-limit 可以完美满足我的需求 https://www.npmjs.com/package/p-limit
现在,我想将当前进度输出给用户。我看到这个库有一个pendingCount属性,但我不知道如何使用它。
任何帮助将不胜感激。
How to use the limit.pendingCount variable in p-limit?有一个类似的问题,但编辑后的答案没有使用pendingCount
我希望能够写“我们仍然缺少 n 个 API 调用”
这是我当前的代码(简化):
import pLimit from 'p-limit';
const aPromise = [];
const urls = [
"https://www.google.com/search?q=1",
"https://www.google.com/search?q=2",
"https://www.google.com/search?q=3",
"https://www.google.com/search?q=4",
"https://www.google.com/search?q=5",
"https://www.google.com/search?q=6",
"https://www.google.com/search?q=7",
"https://www.google.com/search?q=8"
];
const limit = pLimit(3);
for (const url of urls) {
try {
aPromise.push(limit(() => fetch(url)));
} catch (error) {
console.error('An error occurred:', error);
}
}
const res = await Promise.allSettled(aPromise);
for (const x of res) {
if (x.status == "fulfilled") {
//process results
} else if (x.status == "rejected") {
console.log(`${x.reason}`);
}
}
添加解决方案,谢谢西蒙
import pLimit from 'p-limit';
const aPromise = [];
const urls = [
"https://www.google.com/search?q=1",
"https://www.google.com/search?q=2",
"https://www.google.com/search?q=3",
"https://www.google.com/search?q=4",
"https://www.google.com/search?q=5",
"https://www.google.com/search?q=6",
"https://www.google.com/search?q=7",
"https://www.google.com/search?q=8"
];
const limit = pLimit(3);
for (const url of urls) {
try {
aPromise.push(limit(() => fetch(url)));
} catch (error) {
console.error('An error occurred:', error);
}
}
let nProgressInterval = setInterval( () => {
printProgress(Math.round(100 - limit.pendingCount * 100 / aPromise.length))
}, 1000);
const res = await Promise.allSettled(aPromise);
clearInterval(nProgressInterval);
nProgressInterval = null;
for (const x of res) {
if (x.status == "fulfilled") {
//process results
} else if (x.status == "rejected") {
console.log(`${x.reason}`);
}
}
// This will write the progress number in the same position
function printProgress(progress) {
process.stdout.clearLine(0);
process.stdout.cursorTo(0);
process.stdout.write(`Progreso ${progress}%`);
}
添加解决方案,谢谢西蒙
import pLimit from 'p-limit';
const aPromise = [];
const urls = [
"https://www.google.com/search?q=1",
"https://www.google.com/search?q=2",
"https://www.google.com/search?q=3",
"https://www.google.com/search?q=4",
"https://www.google.com/search?q=5",
"https://www.google.com/search?q=6",
"https://www.google.com/search?q=7",
"https://www.google.com/search?q=8"
];
const limit = pLimit(3);
for (const url of urls) {
try {
aPromise.push(limit(() => fetch(url)));
} catch (error) {
console.error('An error occurred:', error);
}
}
let nProgressInterval = setInterval( () => {
printProgress(Math.round(100 - limit.pendingCount * 100 / aPromise.length))
}, 1000);
const res = await Promise.allSettled(aPromise);
clearInterval(nProgressInterval);
nProgressInterval = null;
for (const x of res) {
if (x.status == "fulfilled") {
//process results
} else if (x.status == "rejected") {
console.log(`${x.reason}`);
}
}
// This will write the progress number in the same position
function printProgress(progress) {
process.stdout.clearLine(0);
process.stdout.cursorTo(0);
process.stdout.write(`Progreso ${progress}%`);
}