Lev
Lev

Reputation: 15684

stop the program if promise fails

How can I stop my node server if a promise fails ?

myPromise.then(res => {})
   .catch(err => {
    throw "bad error"  // I want to stop the program like I in the synchronous senario. Not get unhandled promise exception
})

Upvotes: 0

Views: 39

Answers (1)

Kim Nedergaard Clausen
Kim Nedergaard Clausen

Reputation: 232

Call the exit() function on the global process object with a non-zero exit code.

myPromise
  .then((res) => {})
  .catch((err) => process.exit(1));

Read more here: https://nodejs.org/api/process.html#process_process_exit_code

Upvotes: 1

Related Questions