这是我使用'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传递给数组?
答案 0 :(得分:1)
我认为这是因为page.property
没有返回承诺,这意味着使用await page.propperty(xxx, function () {})
没有意义。所以也许你可以尝试包装page.propperty
第一个