Comments (15)
chrome :69 两个事件均无触发
window.addEventListener('unhandledrejection', event =>
{
console.log(event.reason); // 打印"Hello, Fundebug!"
});
window.addEventListener('rejectionhandled', event =>
{
console.log('rejection handled'); // 1秒后打印"rejection handled"
});
function foo()
{
return Promise.reject('Hello, Fundebug!');
}
var r = foo();
setTimeout(() =>
{
r.catch(e =>{});
}, 1000);
from the-front-end-knowledge-you-may-not-know.
当 Promise 发生异常后,会从 .then
链进入到 .catch
链, 不过 Promise 链结束后依然存在异常,既Promise 依然是 reject 状态,Promise 坐在函数会 throw 一个 uncaughtException
异常,此异常一层一层向外传播,如果所有函数都没有捕获,则在 devtools 的 console 输出此错误。
相关阅读
- tc39/proposal-async-await#72
- nodejs/node#830
- domenic/unhandled-rejections-browser-spec
- Try/Catch always needed for await? - esdiscuss
from the-front-end-knowledge-you-may-not-know.
我也遇到了这个问题,发现问题在于引入的js资源于当前网址不同源导致的,unhandledrejection跟随同源策略
from the-front-end-knowledge-you-may-not-know.
如果使用es7的async来写代码
await asyncFunc()
func1()
func2()
此时asyncFunc
reject
了会怎么处理?不执行func1
和func2
并且当成catch
没有捕获?
from the-front-end-knowledge-you-may-not-know.
@erguotou520 跟Promise的方式一样
try catch都能捕获es7的await返回的reject
为啥不统一用window.onerror处理,尴了个尬~~
from the-front-end-knowledge-you-may-not-know.
try catch 写起来反而没promise方式好看,而且有的业务逻辑不适合使用全局onerror处理
from the-front-end-knowledge-you-may-not-know.
@erguotou520
我的意思是,与其新搞一个unhandledrejection事件处理Promise的异常
还不如直接用onerror处理
而且unhandledrejection/onerror应该统一用来处理那些漏掉的异常吧,
什么情况下会写业务逻辑上去。。
from the-front-end-knowledge-you-may-not-know.
@taixw2 我们说的场景不一样,我指的是确实需要处理的异常,属于正常的异常。
按照2位的说法,应该是正常的流程都不用try catch,因为异常(确实是错误的异常)很少走到,这种异常用全局的错误捕获
from the-front-end-knowledge-you-may-not-know.
看了你们的讨论,感觉unhandledrejection
和onerror
事件其实差别不大,都是用来捕获Promise中未捕获的reject异常。
unhandledrejection
和onerror
都是全局事件,正常情况下肯定不能用来写业务逻辑;那这两个事件的作用可能就只剩下“处理异常,不让异常抛出到控制台”,就像我一个同事说的那样,在控制台有大量飘红的输出显得很不专业。
其实个人感觉全局捕获异常不让输出到控制台可能显得干净,但是也不利于开发定位错误;还要一个不好的地方就是,如果要细粒度地针对情况处理每一种异常,就可能导致事件的回调变的很臃肿,执行时间更长。
我觉得最好是在每个Promise后都手动加上catch
,会省去很多不必要的麻烦。而unhandledrejection
和onerror
这种兜底的方案,总感觉不是一个好方案;或者在单页应用中,针对每一个路由加载的页面,分别设置不同的unhandledrejection
和onerror
回调,在dom元素mounted的时候添加到window上,在页面销毁的时候卸载;每个页面都这么操作,既实现了兜底,又避免了全局注册一个回调,导致回调非常臃肿的问题。
from the-front-end-knowledge-you-may-not-know.
https://drafts.csswg.org/css-writing-modes-4/#inline-base-direction
from the-front-end-knowledge-you-may-not-know.
chrome :69 两个事件均无触发
window.addEventListener('unhandledrejection', event => { console.log(event.reason); // 打印"Hello, Fundebug!" }); window.addEventListener('rejectionhandled', event => { console.log('rejection handled'); // 1秒后打印"rejection handled" }); function foo() { return Promise.reject('Hello, Fundebug!'); } var r = foo(); setTimeout(() => { r.catch(e =>{}); }, 1000);
请问找到原因了吗,困惑,是chrome的问题吗?
from the-front-end-knowledge-you-may-not-know.
@w1301625107 这里有提到 https://stackoverflow.com/questions/40026381/unhandledrejection-not-working-in-chrome
代码实际上是有效的.
from the-front-end-knowledge-you-may-not-know.
chrome :69 两个事件均无触发
window.addEventListener('unhandledrejection', event => { console.log(event.reason); // 打印"Hello, Fundebug!" }); window.addEventListener('rejectionhandled', event => { console.log('rejection handled'); // 1秒后打印"rejection handled" }); function foo() { return Promise.reject('Hello, Fundebug!'); } var r = foo(); setTimeout(() => { r.catch(e =>{}); }, 1000);请问找到原因了吗,困惑,是chrome的问题吗?
请问找到原因了吗? 我在vue和angular里作了这个例子打包后运行,angular能正常捕获,vue不能,怀疑是babel版本造成的,但没能找到解决方法。
window.addEventListener('unhandledrejection', event => {
console.error('window.Listener unhandledrejection: ', event.reason);
event.preventDefault();
});
window.addEventListener('rejectionhandled', event => {
console.error('window.Listener rejectionhandled: ', event.reason);
event.preventDefault();
});
window.addEventListener('error', event => {
console.error('window.Listener error: ', event);
event.preventDefault();
});
Promise.reject('Promise reject');
from the-front-end-knowledge-you-may-not-know.
chrome :69 两个事件均无触发
window.addEventListener('unhandledrejection', event => { console.log(event.reason); // 打印"Hello, Fundebug!" }); window.addEventListener('rejectionhandled', event => { console.log('rejection handled'); // 1秒后打印"rejection handled" }); function foo() { return Promise.reject('Hello, Fundebug!'); } var r = foo(); setTimeout(() => { r.catch(e =>{}); }, 1000);请问找到原因了吗,困惑,是chrome的问题吗?
请问找到原因了吗? 我在vue和angular里作了这个例子打包后运行,angular能正常捕获,vue不能,怀疑是babel版本造成的,但没能找到解决方法。
window.addEventListener('unhandledrejection', event => { console.error('window.Listener unhandledrejection: ', event.reason); event.preventDefault(); }); window.addEventListener('rejectionhandled', event => { console.error('window.Listener rejectionhandled: ', event.reason); event.preventDefault(); }); window.addEventListener('error', event => { console.error('window.Listener error: ', event); event.preventDefault(); }); Promise.reject('Promise reject');
看一下core-js的版本,unhandledrejection 只对原生的promise有效,如果是polyfill,看一下polyfill中的promise 该如何捕获
from the-front-end-knowledge-you-may-not-know.
chrome :69 两个事件均无触发
window.addEventListener('unhandledrejection', event => { console.log(event.reason); // 打印"Hello, Fundebug!" }); window.addEventListener('rejectionhandled', event => { console.log('rejection handled'); // 1秒后打印"rejection handled" }); function foo() { return Promise.reject('Hello, Fundebug!'); } var r = foo(); setTimeout(() => { r.catch(e =>{}); }, 1000);
直接在console里执行或者协议是file:///都不会触发,你运行在http localhost下会有输出。
from the-front-end-knowledge-you-may-not-know.
Related Issues (20)
- Async Clipboard API:异步剪贴板 API HOT 3
- window.performance 性能对象 HOT 1
- navigator.vibrate 手机震动API
- Syntax Error of the Title HOT 1
- try-catch 退出 forEach 循环 HOT 10
- 用fetch在控制台测试接口 HOT 3
- 清空 input[type="file"] 的值 HOT 4
- pointer-events: none; 穿透当前元素,由“下面”的元素接收事件 HOT 4
- 利用 URLSearchParams 对象获取URL之中的查询字符串,即问号之后的部分 HOT 6
- 利用toString检测浏览器devTool是否为开启状态 HOT 7
- 面试题,css中哪些属性对页面repaint和reflow影响最大 HOT 4
- Array.isArray(Array.prototype) === true HOT 1
- 怎样阻止谷歌浏览器的密码提示? HOT 15
- 不打印出 console 信息出现在文件哪一行 HOT 1
- PC端 Vue-SPA项目 如何做好首屏渲染优化 HOT 3
- 获取图片原始尺寸 HOT 7
- [Docs] README 错别字 顾名思义
- 小技巧:已知年月,求该月共多少天? HOT 6
- 通过AbortController中止fetch请求后,如何判断返回的error是否属于AbortError? HOT 1
- 如何禁止 chrome 弹窗提示保存密码呢
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from the-front-end-knowledge-you-may-not-know.