【发布时间】:2017-11-06 20:48:38
【问题描述】:
所以我有一个运行良好的噩梦代码,我把它放到了一个类中。然而它开始抛出承诺错误:-((没有 fun() 函数它工作正常。
class test {
constructor() {
this.init(() => {
this.start()
})
}
init() {
this.nightmare = new Nightmare({
show: true,
typeInterval: 20,
openDevTools: {
detach: true
}
});
}
async start() {
await this.nightmare
.useragent(userAgent)
.goto("https://www.yahoo.com")
fun();
async function fun() {
await this.nightmare.goto('https://google.com')
}
}
}
new test().start();
错误是:
(节点:1101)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝 id:1):TypeError:无法读取未定义的属性“噩梦”
(node:1101) [DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。将来,未处理的 Promise 拒绝将使用非零退出代码终止 Node.js 进程。
【问题讨论】:
-
将您的
await this.nightmare.goto('https://google.com')包裹在try catch中 -
你试过
await fun();吗?如果在函数初始化之后移动调用会发生什么?
标签: javascript node.js class promise nightmare