首页 > 解决方案 > 如果 promise 失败则停止程序

问题描述

如果承诺失败,我如何停止我的节点服务器?

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
})

标签: node.jspromise

解决方案


使用非零退出代码调用exit()全局对象上的函数。process

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

在此处阅读更多信息: https ://nodejs.org/api/process.html#process_process_exit_code


推荐阅读