简体   繁体   English

ES6承诺错误处理

[英]ES6 Promises Error Handling

I'm currently reading "YKDJS - Async & Performance" by Kyle Simpson, in particular Chapter 3 - Promises. 我目前正在阅读Kyle Simpson撰写的“ YKDJS-Async&Performance”,尤其是第3章-Promises。

The author says that any Promise for which no rejection handler is registered receives a default one: 作者说,任何未注册拒绝处理程序的Promise都会收到一个默认值:

let p = new Promise(function(resolve, reject) { resolve("Yay!"); });
p.then(
function(val) { /* Do whatever */ } 
   /* Default rejection handler
   , function(e) { throw e; } */
);

Later in the chapter he claims that one problem with the way Promise s are designed is the following: 在本章的后面,他声称Promise的设计方式存在以下问题:

In any Promise chain, any error that happens in one of the handler functions of the last Promise in the chain is just "swallowed" instead of being reported. 在任何Promise链中,链中最后一个Promise的处理函数之一中发生的任何错误都只是被“吞噬”而不是被报告。 He proposes to change the way Promise s work so that any Promise that doesn't have a rejection handler reports the error by throwing a global error. 他建议更改Promise的工作方式,以便任何没有拒绝处理程序的Promise都通过引发全局错误来报告错误。 Then he proposes a theoretical Promise#defer() function one could use on a Promise to prevent this reporting behavior. 然后,他提出了一种理论上的Promise#defer()函数,可以在Promise上使用它来防止这种报告行为。

Now my question is: How do these two go together? 现在我的问题是:两者如何结合在一起? It's true that any Promise that doesn't have a rejection handler receives a default one which just throws the rejection value as a global error: 的确,任何没有拒绝处理程序的Promise都会收到一个默认值,该默认值只会将拒绝值作为全局错误抛出:

Promise.reject("Oops");
/* VM668:1 Uncaught (in promise) Oops */

So already Promise s seem to work in just the way he proposes. 因此Promise似乎已经可以按照他的建议进行工作。 Am I misunderstanding something? 我误会了吗? Thanks for any help. 谢谢你的帮助。

The Uncaught Handling he mentions in 他提到的未抓到的处理”

Some Promise libraries have added methods for registering something like a "global unhandled rejection" handler, which would be called instead of a globally thrown error. 一些Promise库添加了用于注册“全局未处理拒绝”处理程序之类的方法的方法,该方法将被调用,而不是全局引发的错误。 But their solution for how to identify an error as "uncaught" is to have an arbitrary-length timer, say 3 seconds, running from time of rejection. 但是他们关于如何将错误识别为“未捕获”的解决方案是从拒绝时间开始运行一个任意长度的计时器(例如3秒)。 If a Promise is rejected but no error handler is registered before the timer fires, then it's assumed that you won't ever be registering a handler, so it's "uncaught." 如果Promise被拒绝,但在计时器触发之前未注册任何错误处理程序,则假定您永远不会在注册处理程序,因此它是“未捕获的”。

In practice, this has worked well for many libraries, as most usage patterns don't typically call for significant delay between Promise rejection and observation of that rejection. 实际上,这对于许多库来说效果很好,因为大多数使用模式通常并不要求在Promise拒绝和观察该拒绝之间有明显的延迟。

has been standardised as unhandled rejection warnings (not using an arbitrary timer, but firing right away). 已被标准化为未处理的拒绝警告 (不使用任意计时器,而是立即触发)。 It does indeed work quite well. 确实确实工作得很好。

He also states in Chapter 4 of ES6 & Beyond 他还在ES6及更高版本的第4章中声明

[When] we are not listening for that rejection, […] it will be silently held onto for future observation. [当]我们不听取拒绝意见时,[...]它将被默默保留以备将来观察。 If you never observe it by calling a then(..) or catch(..) , then it will go unhandled. 如果您从未通过调用then(..)catch(..)观察它,则它将无法处理。 Some browser developer consoles may detect these unhandled rejections and report them, but this is not reliably guaranteed; 某些浏览器开发人员控制台可能会检测到这些未处理的拒绝并进行报告,但这不能可靠地保证; you should always observe promise rejections. 您应该始终遵守诺言拒绝。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM