Javascript 嵌套等待don';木偶演员不能正常工作

Javascript 嵌套等待don';木偶演员不能正常工作,javascript,puppeteer,Javascript,Puppeteer,在这段代码中,我试图过滤掉ElementHandle数组的一部分。我检查它是否有效的方法是打印最终过滤数组的长度。应该是4,而不是30 const ar = await page.$$("li[class*=react-job-listing]"); const shortArray = Array.from(ar).filter(async (el)=> { console.log((await (await el.getProperty("inne

在这段代码中,我试图过滤掉ElementHandle数组的一部分。我检查它是否有效的方法是打印最终过滤数组的长度。应该是4,而不是30

const ar = await page.$$("li[class*=react-job-listing]");
const shortArray = Array.from(ar).filter(async (el)=> {
    console.log((await (await el.getProperty("innerText")).jsonValue()).includes("Easy Apply"));
    return (await (await el.getProperty("innerText")).jsonValue()).includes("Easy Apply");

});
//console.log((await (await ar[0].getProperty("innerText")).jsonValue()).includes("Easy Apply"));
console.log(shortArray.length);
console.log('hello');
不幸的是,这就是结果

30
hello
false
false
false
false
true
false
false
true
false
false
false
false
false
false
false
false
true
false
false
false
false
false
false
false
false
false
false
false
true
false
该长度的控制台日志出现在过滤器执行之前,此时它应该是最后一件事

看来剧本并不是在等待。这一定是由于多重嵌套等待造成的。但我不知道如何修复它


我知道这真的很难看。但是我现在不能使用page.evaluate和DOM函数有一些原因。现在请检查一下。

这不是木偶演员的错。异步函数返回Promise,因此在
Array.from(ar.filter()
中,每个callbak返回thruthy值,没有任何内容被过滤掉。使用
for..of
循环进行异步操作更简单、更安全:

从“木偶师”导入木偶师;
const browser=wait puppeter.launch();
常量html=`
试验
富1

富2

酒吧1

酒吧2

`; 试一试{ const[page]=wait browser.pages(); wait page.goto(`data:text/html,${html}`); 常数=等待页面。$$(“p”); 常量shortArray=[]; for(ar的常量元素){ const text=await(await元素.getProperty(“innerText”)).jsonValue(); 日志(text,text.includes(“foo”)); if(text.includes(“foo”))shortArray.push(元素); } console.log(shortArray.length); log('hello'); }catch(err){console.error(err);}最后{wait browser.close();}

Array.from(ar).filter
不返回承诺。。。所以你为什么等着呢?大多数其他的
wait
也适用于非承诺-您可能需要
wait Promise.all(Array.from(ar).filter…
来等待来自
Array.includes()的任何承诺。
不会返回承诺…那么…为什么要等待呢?我不是在等待String:includes(),我在等待jsonValue()@JaromandaX你说得对,我把它处理掉了。我想看看它是否有用。它没有…谁在console.log函数中使用wait?你可以尝试
wait(你的代码)。然后(console.log(filter);return value;)
foo 1 true
foo 2 true
bar 1 false
bar 2 false
2
hello