繁体   English   中英

Node.js将变量从事件处理程序返回到父函数

[英]Node.js Return variable from event handler to parent function

我需要函数scrape返回在page.on("request")事件处理程序中获得的值。

async function scrape(url) {
        const page = await browser.newPage();
        await page.setRequestInterception(true);

        page.on("request", async(request) => {
            return "fish"
        }
        await page.goto(url)
    }

目前:

const ans = await scrape(url)
console.log(ans)
"undefined'

预期:

const ans = await scrape(url)
console.log(ans)
"fish"

您需要返回看到正在等待的事件时已解决的承诺

const matchRequest = request => request.method() === 'GET'; // your filter
async function scrape(url) {
  return new Promise(resolve => {
    const page = await browser.newPage();
    // not sure what your logic is, but if you don't need to cancel or modify requests/resposes you probably don't need interception
    // await page.setRequestInterception(true);
    page.on("response", async(response) => {
        if (matchRequest(response.request())) {
           resolve(response.buffer());
        }
    }
    await page.goto(url);
  })
}

const body = await scrape('https://example.com');

尝试按照以下步骤操作:

async function scrape(url) {
    let sendRequest = []
    const browser = await puppeteer.launch()
    const page = await browser.newPage()
    await page.setRequestInterception(true)
    page.on('request', request => {
      request.continue()
      sendRequest.push('fish')
    })
    await page.goto(url)
    return sendRequest
}

使用request.continue()通过可选的请求覆盖继续请求。 要使用此功能,应使用page.setRequestInterception启用请求拦截。 如果未启用请求拦截,则会立即引发异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM