未捕获(承诺)错误 [英] Uncaught (in promise) Error

查看:120
本文介绍了未捕获(承诺)错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这显然是一个SSCCE。我有以下内容(jsFiddle 此处):

This is obviously a SSCCE. I have the following (jsFiddle here):

<html>
  <body>
    <input id='file-dlg' type='file'/>
    <br/>
    <button id='submit' type='button'>submit</button>
    <script>
     document.getElementById('file-dlg').addEventListener('change', storeAPromise);
     var p; 
     function storeAPromise() {
       p = new Promise(function executor(resolve, reject) {
         try {
           throw new Error('snafu');
         } catch(e) {
           reject(e);
         }
       });
     };
     document.getElementById('submit').onclick = function() {
       p.then(function() {}, function reject(e) {
         console.error('some problem happenned', e);
       });
     };
    </script>
  </body>
</html>

当用户使用文件对话框选择文件时,我希望什么都不打印在控制台上捕获错误并调用promise的 reject 函数。相比之下,我希望只有当我点击提交按钮时才会在控制台上显示错误,并显示发生了一些错误。

When the user is using the file dialog to select a file, I expect nothing at all to be printed on the console as the Error is caught and the promise's reject function is called. In contrast, I am expecting the error to appear on the console with the description "some error happened" only when I click the "submit" button.

然而,这是不是我观察到的一旦用户选择了我在控制台上看到的对话框的文件:

Yet, this is not what I observe. As soon as the user selects a file with the dialog I see on the console:

Uncaught (in promise) Error: snafu(…)

当用户按下提交按钮时,我看到预期的日志行有些问题发生了但我不明白为什么当用户选择带有文件对话框的文件时,我也会看到较早的未捕获(在承诺中)日志行。我也不明白为什么错误被描述为未捕获,因为我捕获(无条件地)所有异常并且简单地调用拒绝函数。

When the user presses the "submit" button I do see the expected log line "some problem happened" but I don't understand why I also see the earlier "Uncaught (in promise)" log line when the user selects a file with the file dialog. I also don't see why the error is described as "Uncaught" given that I catch (unconditionally) all exceptions and simple invoke the reject function.

推荐答案

这不是未被捕获的异常,它是未被捕获的拒绝。 Promise不应该在被拒绝状态下悬挂而没有附加错误回调 - 这就是控制台警告你的原因。您还可以使用 在您的应用程序中处理这些案例。 unhandledrejection 事件

It's not an uncaught exception, it's an uncaught rejection. Promises should not be left dangling in the rejected state with no error callback attached - that's why the console is warning you about this. You can also handle these cases in your application with the unhandledrejection event.

如果你想稍后处理错误(当用户点击按钮时安装处理程序,可能会从来没有!),你仍然需要立即安装一个空回调(明确忽略错误)来抑制警告。

If you want to handle the error later (install the handler when the user clicks the button, which might be never!), you still will need to immediately install an empty callback (that explicitly ignores the error) to suppress the warning.

var p = Promise.resolve();
document.getElementById('file-dlg').addEventListener('change', function createPromise() {
  p = new Promise(function executor(resolve, reject) {
    throw new Error('snafu');
  });
  p.catch(e => {/* ignore for now */});
});
document.getElementById('submit').addEventListener('click', function addErrorHandler() {
  p.catch(function onReject(e) {
    console.error('some problem happened', e);
  });
});

请注意,您不需要尝试 / catch - 默认情况下,promise构造函数将捕获执行程序中的所有异常。

Notice that you don't need that try/catch - the promise constructor will catch all exceptions from the executor by default already.

这篇关于未捕获(承诺)错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆