nodejs 捕获 promise 未处理的 reject
2016-09-23 11:58
417 查看
正常情况下,对于没有捕获的 promise的reject 会直接静默的吃掉.而这不是我们想要的.
复现一下看看function cb(){ console.log('444'); fdsaf.fdafdas = 777; } new Promise((resolve,reject)=>{ cb(1) })
解决:
文档
https://nodejs.org/dist/latest-v4.x/docs/api/process.html#process_event_unhandledrejectionprocess.on('unhandledRejection', (reason, p) => { console.log("Unhandled Rejection at: Promise ", p, " reason: ", reason); // application specific logging, throwing an error, or other logic here });
toy code
process.on('unhandledRejection', (reason, p) => { console.log("Unhandled Rejection at: Promise ", p, " reason: ", reason); // application specific logging, throwing an error, or other logic here });
function cb(){ console.log('444'); fdsaf.fdafdas = 777; } new Promise((resolve,reject)=>{ cb(1) })
toy code2 co
process.on('unhandledRejection', (reason, p) => { console.log("Unhandled Rejection at: Promise ", p, " reason: ", reason.stack); // application specific logging, throwing an error, or other logic here }); var co = require('co'); function cb(){ console.log('444'); fdsaf.fdafdas = 777; } co(function*(){ console.log('1111'); throw 'myerror```'; }).then(function(value) { console.log(value); // Success! }).catch(err=>{ console.log('222'); console.log(err); // Error! cb(); console.log('!!!!'); }).catch(err=>{ console.log('aaa :',err); })
nodejs 6.6以后.对没有捕获的 reject 会发出一个警告.
promises: Unhandled rejections now emit a process warning after the first tick. (Benjamin Gruenbaum) #8223
相关文章推荐
- 捕获未处理的Promise错误
- 捕获未处理的Promise错误方法
- nodejs 捕获未处理异常
- 捕获未处理的Promise错误
- Promise.all中对于reject的处理方法
- 异常的捕获及处理
- 通过spring的aop注解实现所有方法异常的捕获处理,sqlite SQLITE_BUSY异常
- iphone 异常捕获处理
- [javascript高手之路] es6系列教程 - promise常见用法详解(resolve,reject,catch,then,all,race)
- Nodejs-使用 promise 替代回调函数
- 体面地处理程序的未捕获异常
- Python异常捕获与处理
- 黑马程序员——Java 异常的捕获及处理
- Nodejs基础:路径处理模块path总结
- nodejs - Promise 用法与面试题分析
- iOS捕获异常,常用的异常处理方法
- 浅谈-----异常的捕获和处理(一)
- 对异常捕获的处理
- Java中的异常处理:何时抛出异常,何时捕获异常?
- 2017.09.01异常的捕获及处理