Answer the question
In order to leave comments, you need to log in
How to make normal throw instead of unhandledRejection in NodeJS?
let go = async () => {
throw new Error('Some Error');
}
go();
(node:14892) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Some Error
(node:14892) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejec
tions that are not handled will terminate the Node.js process with a non-zero exit code.
Answer the question
In order to leave comments, you need to log in
The answer to your question is in the off documentation
. The cause of the "unhandledRejection" event is also described there.
In general, there are several solutions to this problem, for example, as suggested by Sergey and Vladlen Hellsight here in the answer.
Other options:
let go = async () => {
throw new Error('Some Error');
}
// можно выловить ошибку в другой async функции
(async () => {
try{
await go()
}
catch(error){
console.error(error); // напечатать лог
process.exit(1); // вернуть код завершения отличный от нуля
}
})()
// Или повесить catch на функцию `go` , это тоже сработает
go().catch(error => {
console.error(error); // напечатать лог
process.exit(1); // вернуть код завершения отличный от нуля
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question