Promise 在puppeter脚本的page.evaluate返回块中使用箭头函数
这可能是一个承诺问题: 查看我的返回块的Promise 在puppeter脚本的page.evaluate返回块中使用箭头函数,promise,puppeteer,arrow-functions,Promise,Puppeteer,Arrow Functions,这可能是一个承诺问题: 查看我的返回块的自字段, 使用箭头函数不会返回任何结果: { link: 'www.xxxxxx.com/1234', name: 'jhon doe', since: {} }, 而是直接返回值,它按预期工作 因为我需要用选择器执行复杂的操作,所以我想在这一点上使用一个内联箭头函数,我如何修正以得到结果 let rawMembers = await page.evaluate(() => new Promise((resolve) =
自字段,
使用箭头函数不会返回任何结果:
{
link: 'www.xxxxxx.com/1234',
name: 'jhon doe',
since: {}
},
而是直接返回值,它按预期工作
因为我需要用选择器执行复杂的操作,所以我想在这一点上使用一个内联箭头函数,我如何修正以得到结果
let rawMembers = await page.evaluate(() => new Promise((resolve) => {
....
//captute all the link
const anchors = Array.from(document.querySelectorAll('a'));
let result = anchors.map(x => {
return {
link: x.getAttribute("href"),
name: x.innerText,
//since : x.parentNode.parentNode.parentNode.parentNode.getAttribute("class") <--- this works
since: x => { <---using an arrow function, it returns and empty objecy `{}`
// i need a function here to do complex and multiline operations
return x.parentNode.parentNode.parentNode.parentNode.getAttribute("class");
}
....
resolve(results);
在中,因为
引用的是箭头函数本身,而不是其结果。函数不可序列化,因此会得到一个空对象。您需要调用该函数,即使用:
自:(()=>{
返回x.parentNode.parentNode.parentNode.parentNode.getAttribute(“类”);
})()
太好了,谢谢!
since: x => new Promise((resolve) => {
// i need a function here to do complex and multiline operations
resolve(x.parentNode.parentNode.parentNode.parentNode.getAttribute("class"));
})