如何使用async / await方法将对象传递到幻像js页面?

时间:2017-04-28 08:28:15

标签: 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
}

如何将msg传递给数组?

1 个答案:

答案 0 :(得分:1)

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