onLoad () {
this.testScript()
}
async testScript(){
cc.log("bg1 = ", typeof bg)
// 该处为错误逻辑,如果注释掉此处,则 testPromise 中的错误可以正常抛出;反之则直接断开
let bg = this.node.getChildByName("bgbgbg").getComponent(cc.Sprite)
cc.log("bg2 = ", typeof bg)
let a = await this.testPromise("111").then(a => a).catch(error => error)
cc.log("a = ", a)
}
testPromise(str:string){
let ret = new Promise((resolve,reject) =>{
setTimeout(() => {
let bg = this.node.getChildByName("bgbgbg").getComponent(cc.Sprite)
cc.log("bg2 = ", typeof bg)
resolve(str)
}, 1000)
})
return ret
}
2.4.0 版本的。
经过测试发现,异步函数中以及异步函数间接调用的函数中,以及整个处于调用栈中的函数中报错,在模拟器中都不会抛出,浏览器会抛出,这块的功能差异是不是有bug啊,当然可以用trycatch捕获,但大量写trycatch很不合理啊,导致代码很乱,还有发现ts对于异步函数会编译成这种形式,如果实在解决不了,能否在编译的时候在红框处自动为异步函数加入trycatch来解决这个问题呢