英文:
Javascript seems to skip the asynchronous parts of my function
问题
我不完全知道我在做什么,所以如果这是一个愚蠢的问题,我很抱歉,但我已经尝试修复这个问题,但在这个可恶的异步问题上一直没有取得进展。我正在尝试制作一个爬虫来制作自己的词频列表,纯粹是为了好玩,我试图收集页面上的链接。以下是修改后的代码:
async function linkChain (depth, already) {
if (!depth) { return; }
let promises = [];
let length = links.length;
for (already; already < length; already++) {
promises.push(
axios.get(www + links[already]).then(response => {
// (在这里做一些操作,将链接添加到 links 数组)
console.log(links.length); // (它在工作)
})
);
}
Promise.all(promises).then(() => {
linkChain(depth - 1, already);
});
}
async function run () {
await linkChain(2, 0);
console.log("here");
}
run();
cheerio 和 axios 部分已经修复并正常工作。links 数组接近一百万。但在记录 links.length 之前,控制台会输出 "here"。
我尝试过使用 .then.catch 而不是 await 和 async,我尝试了很多愚蠢的方法,我尝试搜索 Stack Overflow(虽然可能错过了解决方案,我不熟悉这个网站)。
我已经删除了一些多余的 return 语句;我以为它们可能会有帮助,但当它们没有帮助时,我尝试了其他方法,然后忘记删除它们。一些愚蠢的尝试。
英文:
I don't entirely know what I'm doing, so sorry if this is a dumb question, but I've tried to fix this, and I haven't made progress on this damn async. I'm trying to make a scraper to make my own word frequency list, just for the hell of it, and I'm trying to collect links to pages off of pages. Here's the revised code:
async function linkChain (depth, already) {
if (!depth) { return; }
let promises = [];
let length = links.length;
for (already; already < length; already++) {
promises.push(
axios.get(www + links[already]).then(response => {
// (some stuff where I push to links)
console.log(links.length); // (It's working)
})
);
}
Promise.all(promises).then(() => {
linkChain(depth - 1, already);
});
}
async function run () {
await linkChain(2, 0);
console.log("here");
}
run();
The cheerio and axios stuff is fixed and working. Links is growing close to a million. But before links.length is console.logged, the console logs "here".
I've tried using .then.catch instead of await and async, I've tried a lot of stuff that was dumb, I've tried searching Stack Overflow (although I may have missed a solution, I'm not familiar with the website).
I have removed some extraneous returns; I thought they might help, and when they didn't I tried something else and forgot to remove them. Some of the dumb stuff.
答案1
得分: 1
linkChain
函数没有返回任何内容,并且您将其标记为 async
,却没有使用 await
等待任何内容,这有点没有意义。
所以,您可以选择要么使用 await Promise.all
,要么不使用 async
关键字并返回它。您还应该确保通过使用返回语句或删除括号来返回内部的 linkChain
调用。
async function linkChain(depth, already) {
// ...
await Promise.all(promises).then(() => {
return linkChain(depth - 1, already);
});
}
或
async function linkChain(depth, already) {
// ...
await Promise.all(promises).then(() => linkChain(depth - 1, already));
}
function linkChain(depth, already) {
// ...
return Promise.all(promises).then(() => {
return linkChain(depth - 1, already);
});
}
或
function linkChain(depth, already) {
// ...
return Promise.all(promises).then(() => linkChain(depth - 1, already));
}
英文:
linkChain
doesn't return anything - and you've marked it async
without ever await
ing anything which is kinda pointless.
So, either await Promise.all
or return it (without the async
keyword). You should also make sure that you return the inner linkChain
call by either using return statement or removing the braces.
1.
async function linkChain (depth, already) {
...
await Promise.all(promises).then(() => {
return linkChain(depth - 1, already);
});
}
or
async function linkChain (depth, already) {
...
await Promise.all(promises).then(() => linkChain(depth - 1, already));
}
2.
function linkChain (depth, already) {
...
return Promise.all(promises).then(() => {
return linkChain(depth - 1, already);
});
}
or
function linkChain (depth, already) {
...
return Promise.all(promises).then(() => linkChain(depth - 1, already));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论