这是我使用“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/