aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/window/rejectionhandled_event/index.html
blob: cf04dd5ae88e8445774159df12f66ba01385843f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
---
title: 'Window: rejectionhandled event'
slug: Web/API/Window/rejectionhandled_event
translation_of: Web/API/Window/rejectionhandled_event
---
<div>{{APIRef("HTML DOM")}}</div>

<p><span class="seoSummary"><code>当Promise被rejected且有rejection处理器时会在全局触发</code><strong><code>rejectionhandled</code></strong> 事件(通常是发生在window下,但是也可能发生在Worker中)。应用于调试一般应用回退。</span>当Promise被rejected且没有rejection处理器处理时会触发<strong>unhandledrejection</strong><span class="seoSummary"><code>事件。这两个事件协同工作。</code></span></p>

<table class="properties">
 <tbody>
  <tr>
   <th scope="row">是否冒泡</th>
   <td>No</td>
  </tr>
  <tr>
   <th scope="row">是否可取消</th>
   <td>No</td>
  </tr>
  <tr>
   <th scope="row">接口</th>
   <td>PromiseRejectionEvent</td>
  </tr>
  <tr>
   <th scope="row">事件处理器属性</th>
   <td>onrejectionhandled</td>
  </tr>
 </tbody>
</table>

<h2 id="例子">例子</h2>

<p>你可以使用<code>rejectionhandled事件在控制台打印出被rejected的Promise,以及被rejected的原因:</code></p>

<pre class="brush: js">window.addEventListener("rejectionhandled", event =&gt; {
  console.log("Promise rejected; reason: " + event.reason);
}, false);
</pre>

<h2 id="说明">说明</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('HTML WHATWG', 'webappapis.html#unhandled-promise-rejections', 'rejectionhandled')}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>



<p>{{Compat("api.Window.rejectionhandled_event")}}</p>

<h2 id="参阅">参阅</h2>

<ul>
 <li>{{SectionOnPage("/en-US/docs/Web/JavaScript/Guide/Using_promises", "Promise rejection events")}}</li>
 <li>{{domxref("PromiseRejectionEvent")}}</li>
 <li>{{domxref("Promise")}}</li>
 <li>{{Event("unhandledrejection")}}</li>
</ul>