gpt4 book ai didi

javascript - 尝试...异步捕获等待不捕获错误

转载 作者:搜寻专家 更新时间:2023-10-31 22:49:17 26 4
gpt4 key购买 nike

<分区>

为什么 try...catch 不适用于以下示例代码?

const http2 = require("http2")
const url = require("url")


function sendRequest() {
return new Promise((resolve, reject) => {

var r = http2.request({
"host": "www.google.com",
"method": "GET",
"path": "/"
}, (resp) => {
var data = []
resp.on("data", (chunk) => {
throw ("Error")
})
resp.on("end", () => {
console.log("ended")
resolve("finished")
})
resp.on("error", (e) => {
console.log("error")
reject(e)
})
})
r.end()
})
}

async function wrapper(){
try {
console.log("Sending request")
await sendRequest()
console.log("Finished sending Request")
}catch(e){
console.log("error!") // Supposed to see this
console.log(e)
}
console.log("All finished") // Supposed to see this
}

wrapper()

输出如下:

Sending request

/Users/test-user/test.js:15
throw ("Error")
^
Error

Process finished with exit code 1

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com