javascript - 如何使用 async/await 方法将对象传递到 phantom js 页面?

标签 javascript node.js asynchronous phantomjs

这是我使用“node --harmony-async-await run”运行的脚本

我想将日志收集到“logs”数组中。但该数组在await 函数内未定义。

const phantomjs = async ({ development, script }) => {
  const instance = await phantom.create()
  const page = await instance.createPage()
  const logs = []

  await page.property('onConsoleMessage', function(msg, lineNum, sourceId) {
    console.log('Console:', msg)
    logs.push(msg) // => will be not executed. logs is undefined here
  })
  ....some other actions
}

如何将消息传递到数组中?

最佳答案

我认为是因为page.property没有返回promise,这意味着没有意义使用await page.propperty(xxx, function () {}).所以也许你可以尝试先包装你的 page.propperty

关于javascript - 如何使用 async/await 方法将对象传递到 phantom js 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43675563/

相关文章:

c# - 从异步回调调用同步方法调用?

asynchronous - 在 Vue 中异步调用后将数据传递给 Prop

javascript - this.id 在声明的函数内部时返回 undefined

javascript - 如何从 for 循环的每次迭代返回值

css - 使用 sass Node 命令行解析来自 node_modules 的 @use 导入?

javascript - 产品 ID 代码不正确地传输到 Shopify 结帐页面

c - 我的程序在扫描完成之前开始循环

javascript - 是否使用 ".click()"异步触发事件监听器?

asp.net - 在 VS2008 中重用 Javascript 例程的最佳方法是什么?

node.js - nodejs中使用express和passport和mongodb的简单登录页面