node.js - 如果 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
})
解决方案
使用非零退出代码调用exit()
全局对象上的函数。process
myPromise
.then((res) => {})
.catch((err) => process.exit(1));
在此处阅读更多信息: https ://nodejs.org/api/process.html#process_process_exit_code
推荐阅读
- keycloak - Keycloak - 获得用户角色的最佳方式是什么
- java - 如何在像 wats 应用程序这样的片段中显示数据时更新选项卡布局徽章计数?
- java - Sonar + Lombok 对 @Data 注释的误报
- javascript - Reactjs 中的 map() 函数不适用于状态
- css - 将跨度文本与 .btn 对齐
- javascript - 如何仅接受来自我的 API 服务器的 cookie
- node.js - NestJS - “SyntaxError: Unexpected token const” while “npm run start:prod”
- angular - 是否可以以某种方式更改接收日期的格式?
- node.js - 如何在节点表情符号中添加自定义表情符号(在节点模块中)
- java - 有人可以告诉我代码中的错误吗?