2019-04-21 11:50:48 +08:00

119 lines
5.9 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<article id="wikiArticle">
<div></div>
<h2 id="Summary" name="Summary">概述</h2>
<p><code><strong>Promise.reject(reason)</strong></code>方法返回一个带有拒绝原因reason参数的Promise对象。</p>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre><code class="language-javascript"><var>Promise.reject(reason)</var>;</code></pre>
<h3 id="Parameters">Parameters</h3>
<dl>
<dt>reason</dt>
<dd>表示<code>Promise</code>被拒绝的原因。</dd>
</dl>
<h3 id="Return_value">Return value</h3>
<dl>
<dd>一个给定原因了的被拒绝的 <a href="Reference/Global_Objects/Promise" title="Promise 对象用于表示一个异步操作的最终状态完成或失败以及该异步操作的结果值。"><code>Promise</code></a></dd>
</dl>
<h2 id="Description" name="Description">描述</h2>
<p><font face="Open Sans, sans-serif">静态函数</font><code>Promise.reject</code>返回一个被拒绝的<code>Promise对象</code>。通过使用<a href="Reference/Global_Objects/Error" title="通过Error的构造器可以创建一个错误对象。当运行时错误产生时Error的实例对象会被抛出。Error对象也可用于用户自定义的异常的基础对象。下面列出了各种内建的标准错误类型。"><code>Error</code></a>的实例获取错误原因<code>reason</code>对调试和选择性错误捕捉很有帮助。</p>
<h2 id="示例">示例</h2>
<h3 id="使用静态Promise.reject()方法">使用静态<code>Promise.reject()</code>方法</h3>
<pre><code class="language-javascript">Promise.reject("Testing static reject").then(function(reason) {
// 未被调用
}, function(reason) {
console.log(reason); // "Testing static reject"
});
Promise.reject(new Error("fail")).then(function(result) {
// 未被调用
}, function(error) {
console.log(error); // stacktrace
});</code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">规范</th>
<th scope="col">状态</th>
<th scope="col">备注</th>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-promise.reject" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">Promise.reject</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td>ECMA规范的首次定义</td>
</tr>
<tr>
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-promise.reject" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">Promise.reject</small></a></td>
<td><span class="spec-Draft">Draft</span></td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<h2 id="We're_converting_our_compatibility_data_into_a_machine-readable_JSON_format._This_compatibility_table_still_uses_the_old_format_because_we_haven't_yet_converted_the_data_it_contains._Find_out_how_you_can_help!_Desktop_Mobile"><span style="font-size: 14px; font-weight: normal; line-height: 1.5;"><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
<strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
<div class="htab">
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
<ul>
<li class="selected"><a>Desktop</a></li>
<li><a>Mobile</a></li>
</ul>
</div></span></h2>
<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>32</td>
<td><a href="/en-US/Firefox/Releases/24" title="Released on 2013-09-17.">24.0</a> (24.0) as <code>Future</code><br/>
<a href="/en-US/Firefox/Releases/25" title="Released on 2013-10-29.">25.0</a> (25.0) as <code>Promise</code> behind a flag[1]<br/>
<a href="/en-US/Firefox/Releases/29" title="Released on 2014-04-29.">29.0</a> (29.0) by default</td>
<td><span style="color: #f00;">未实现</span></td>
<td>19</td>
<td>7.1</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>
<th>Chrome for Android</th>
</tr>
<tr>
<td>Basic support</td>
<td><span style="color: #f00;">未实现</span></td>
<td>24.0 (24.0) as <code>Future</code><br/>
25.0 (25.0) as <code>Promise</code> behind a flag[1]<br/>
29.0 (29.0) by default</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td>iOS 8</td>
<td>32</td>
</tr>
</tbody>
</table>
</div>
<p>[1] Gecko 24<span style="font-family: Consolas,Monaco,'Andale Mono',monospace;">实验性地实施了</span><code style="font-style: normal;">Promise类</code>最初被命名为<code style="font-style: normal;">Future</code>在Gecko 25被重新命名为现在的名字 但在<code style="font-style: normal;">dom.promise.enabled设置中被默认禁用。</code> <a class="external external-icon" href="https://bugzilla.mozilla.org/show_bug.cgi?id=918806" rel="noopener">Bug 918806</a> Gecko 29开始默认启用Promise。</p>
<h2 id="参见">参见</h2>
<ul>
<li><a href="Reference/Global_Objects/Promise" title="Promise 对象用于表示一个异步操作的最终状态完成或失败以及该异步操作的结果值。"><code>Promise</code></a></li>
<li><a class="external" href="https://github.com/petkaantonov/bluebird#error-handling" rel="noopener">使用BlueBird Promise库进行选择性错误捕捉</a></li>
</ul>
</article>