Node.js Error Handling
Q:- How to handle error/exceptions with Promises in Node.js?
First way, by using promises you can chain different operations, and handle errors at the end.
doSomething() .then(doSomething1()) .then(doSomething2()) .catch(err => console.error(err))
Second way, by using promises you can handle errors locally.
doSomething .then((() => { return doSomething1().catch(err => { //handle error throw err //break the chain! }) }) .then((() => { return doSomething2().catch(err => { //handle error throw err //break the chain! }) }) .catch(err => console.error(err))