0
async
함수에서 발생한 오류를 어떻게 catch 할 수 있습니까? 아래에있는 내 예와 같이 : 나는 async
의 try-catch
외부를 넣어 경우nodejs - 더 깊은 레벨에서 오류 잡기
I) 예를 들어 작업 잡을 (오류)
(async() => {
try {
// do some await functions
throw new Error("error1")
}
catch(e) {
console.log(e)
}
})()
콘솔
II
Error: error1
at __dirname (/home/test.js:25:11)
at Object.<anonymous> (/home/quan/nodejs/IoT/test.js:30:3)
at Module._compile (module.js:624:30)
at Object.Module._extensions..js (module.js:635:10)
at Module.load (module.js:545:32)
at tryModuleLoad (module.js:508:12)
at Function.Module._load (module.js:500:3)
at Function.Module.runMain (module.js:665:10)
at startup (bootstrap_node.js:201:16)
at bootstrap_node.js:626:3
) 그러나, 예외가 같은 catch 할 수없는된다 아래 :
try {
(async() => {
throw new Error("error1")
})()
}
catch(e) {
console.log(e)
}
콘솔 :
(node:3494) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: error1
(node:3494) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
II에서 설명한대로 async
에서 발생하는 오류를 catch 할 수있는 방법이 있습니까?
switch-case
이 많은 코드를 단순화하기 위해이 질문을해야하며 각각 switch-case
에 try-catch
을 처리하고 싶지 않습니다. 당신은이 문제를 해결 약속 체인의 끝에 캐치를 추가하기 위해 약속을 사용할 수 있습니다