diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/promiserejectionevent | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/promiserejectionevent')
3 files changed, 304 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/promiserejectionevent/index.html b/files/zh-cn/web/api/promiserejectionevent/index.html new file mode 100644 index 0000000000..997aa87b43 --- /dev/null +++ b/files/zh-cn/web/api/promiserejectionevent/index.html @@ -0,0 +1,124 @@ +--- +title: PromiseRejectionEvent +slug: Web/API/PromiseRejectionEvent +translation_of: Web/API/PromiseRejectionEvent +--- +<div>{{APIRef("HTML DOM")}}</div> + +<p><strong><code>PromiseRejectionEvent</code></strong> 接口表示出现在JavaScript {{jsxref("Promise")}}s 被rejecte (拒绝) 时触发的事件。这些事件对遥测(远程测试)和调试特别的有用。</p> + +<h2 id="构造函数">构造函数</h2> + +<dl> + <dt>{{domxref("PromiseRejectionEvent.PromiseRejectionEvent", "PromiseRejectionEvent()")}}</dt> + <dd>用给定的参数生成一个 <code>PromiseRejectionEvent</code> 事件。</dd> +</dl> + +<h2 id="属性">属性</h2> + +<p><em>也从它的父级{{domxref("Event")}}继承属性。</em></p> + +<dl> + <dt>{{domxref("PromiseRejectionEvent.promise")}} {{readOnlyInline}}</dt> + <dd>被 rejected 的 JavaScript {{jsxref("Promise")}} 。</dd> + <dt>{{domxref("PromiseRejectionEvent.reason")}} {{readOnlyInline}}</dt> + <dd>一个值或 {{jsxref("Object")}} 表明为什么 promise 被 rejected,并传递给{{jsxref("Promise.reject()")}}。</dd> +</dl> + +<h2 id="方法">方法</h2> + +<p><em>没有特定的方法; 从它的父级 {{domxref("Event")}}继承方法。</em></p> + +<h2 id="事件">事件</h2> + +<dl> + <dt>{{Event("unhandledrejection")}}</dt> + <dd>在一个JavaScript {{jsxref("Promise")}} 被 reject(拒绝) 但是没有 reject 处理函数来处理时触发。</dd> + <dt>{{Event("rejectionhandled")}}</dt> + <dd>在一个JavaScript {{jsxref("Promise")}} 被 reject 时触发,在 reject 后由promise的 reject 处理函数处理。 </dd> +</dl> + +<h2 id="例子">例子</h2> + +<pre class="brush: js">window.onunhandledrejection = function(e) { + console.log(e.reason); +}</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#promiserejectionevent', 'PromiseRejectionEvent')}}</td> + <td>{{ Spec2('HTML WHATWG') }}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>功能</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>基本支持</td> + <td>49</td> + <td>{{CompatNo}}<sup>[1]</sup></td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>功能</th> + <th>Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>基本支持</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}<sup>[1]</sup></td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<p>[1] 在 Firefox 里,有实现这个接口但是默认是禁用的。要打开它的话,去到about:config 将 <code>dom.promise_rejection_events.enabled 启用为真。</code></p> + +<h2 id="参见">参见</h2> + +<ul> + <li>{{jsxref("Promise")}}</li> + <li>{{domxref("WindowEventHandlers.onrejectionhandled")}}</li> + <li>{{domxref("WindowEventHandlers.onunhandledrejection")}}</li> +</ul> diff --git a/files/zh-cn/web/api/promiserejectionevent/promise/index.html b/files/zh-cn/web/api/promiserejectionevent/promise/index.html new file mode 100644 index 0000000000..d539826ce8 --- /dev/null +++ b/files/zh-cn/web/api/promiserejectionevent/promise/index.html @@ -0,0 +1,66 @@ +--- +title: PromiseRejectionEvent.promise +slug: Web/API/PromiseRejectionEvent/promise +translation_of: Web/API/PromiseRejectionEvent/promise +--- +<div>{{APIRef("HTML DOM") }}</div> + +<p> {{domxref("PromiseRejectionEvent")}}事件对象的<code><strong>promise</strong></code>属性是只读的,表明Promise被reject的原因。您可以通过检查{{domxref("PromiseRejectionEvent.reason")}}来了解Promise为什么被reject。</p> + +<h2 id="Syntax">Syntax</h2> + +<pre class="syntaxbox"><em>promise</em> = <em>PromiseRejectionEvent</em>.promise</pre> + +<h3 id="Value">Value</h3> + +<p>一个被reject的,并且错误未被处理的{{jsxref("Promise")}} </p> + +<h2 id="Examples">Examples</h2> + +<p>下面的例子监听了未被处理的promise,如果{{domxref("PromiseRejectionEvent.reason", "reason")}} 是一个对象,并且其<code>code</code>属性包含了一段文本“Module not read.”,一个空闲的回调函数被声明,当任务执行错误时会进行重试</p> + +<p>{{domxref("event.preventDefault()")}} 用来表明该promise已被处理</p> + +<pre class="brush: js">window.onunhandledrejection = function(event) { + if (event.reason.code && event.reason.code == "Module not ready") { + window.requestIdleCallback(function(deadline) { + loadModule(event.reason.moduleName) + .then(performStartup); + }); + event.preventDefault(); + } +}</pre> + +<h2 id="Specifications">Specifications</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#dom-promiserejectionevent-promise', 'PromiseRejectionEvent.promise')}}</td> + <td>{{ Spec2('HTML WHATWG') }}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("api.PromiseRejectionEvent.promise")}}</p> + +<h2 id="See_also">See also</h2> + +<ul> + <li>{{jsxref("Promise")}}</li> + <li>{{domxref("PromiseRejectionEvent")}}</li> + <li>{{event("rejectionhandled")}}</li> + <li>{{event("unhandledrejection")}}</li> +</ul> diff --git a/files/zh-cn/web/api/promiserejectionevent/promiserejectionevent/index.html b/files/zh-cn/web/api/promiserejectionevent/promiserejectionevent/index.html new file mode 100644 index 0000000000..6d6e1e3d92 --- /dev/null +++ b/files/zh-cn/web/api/promiserejectionevent/promiserejectionevent/index.html @@ -0,0 +1,114 @@ +--- +title: PromiseRejectionEvent.PromiseRejectionEvent() +slug: Web/API/PromiseRejectionEvent/PromiseRejectionEvent +translation_of: Web/API/PromiseRejectionEvent/PromiseRejectionEvent +--- +<div>{{APIRef("HTML DOM")}}</div> + +<p><code><strong>PromiseRejectionEvent()</strong></code> 构造器返回一个新创建的 {{domxref("PromiseRejectionEvent")}},代表一个JavaScript {{jsxref("Promise")}}被rejected时触发的事件。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">new PromiseRejectionEvent(<em>type</em>, { + promise: <em>somePromise</em>, + reason : <em>someValue</em> +}); +</pre> + +<h3 id="参数">参数</h3> + +<p><em><code>PromiseRejectionEvent()构造函数继承了</code>{{domxref("Event.Event", "Event()")}}的参数。</em></p> + +<dl> + <dt><code>type</code></dt> + <dd>一个代表PromiseRejectionEvent的类型名称的字符串。这是区分大小写的同时必须是{{event("rejectionhandled", '"rejectionhandled"')}} 或者 {{event("unhandledrejection", '"unhandledrejection"')}} 其中之一。</dd> + <dt><code>promise</code></dt> + <dd>代表被 rejected 的{{jsxref("Promise")}}。</dd> + <dt><code>reason</code></dt> + <dd>代表 promise 被 rejected的原因的值或者对象{{jsxref("Object")}} 。</dd> +</dl> + +<h2 id="例子">例子</h2> + +<pre class="brush: js">var myRejectionEvent = new PromiseRejectionEvent('unhandledrejection', { + promise : myPromise, + reason : 'My house is on fire' +});</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#promiserejectionevent', 'PromiseRejectionEvent()')}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>49</td> + <td>{{CompatNo}}<sup>[1]</sup></td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}<sup>[1]</sup></td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<p>[1] 在Firefox里,实现了这个构造函数但是默认是禁用的。为了打开它,需去到 about:config 然后将 <code>dom.promise_rejection_events.enabled</code> 设置启用为真。</p> + +<h2 id="另请参阅">另请参阅</h2> + +<ul> + <li>{{jsxref("Promise")}}</li> + <li>{{domxref("PromiseRejectionEvent")}}</li> +</ul> |