blob: d733feaa6a77bb910306a2d89fc0bf74fde088c2 (
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
73
74
75
76
77
78
|
---
title: Promise.reject()
slug: Web/JavaScript/Reference/Global_Objects/Promise/reject
tags:
- ECAMScript 2015
- JavaScript
- Method
- Promise
translation_of: Web/JavaScript/Reference/Global_Objects/Promise/reject
---
<div>{{JSRef}}</div>
<p><code><strong>Promise.reject()</strong></code>方法返回一个带有拒绝原因的<code>Promise</code>对象。</p>
<p>{{EmbedInteractiveExample("pages/js/promise-reject.html")}}</p>
<h2 id="Syntax">语法</h2>
<pre class="syntaxbox"><var>Promise.reject(reason)</var>;</pre>
<h3 id="参数">参数</h3>
<dl>
<dt>reason</dt>
<dd>表示<code>Promise</code>被拒绝的原因。</dd>
</dl>
<h3 id="返回值">返回值</h3>
<dl>
<dd>一个给定原因了的被拒绝的 {{jsxref("Promise")}}。</dd>
</dl>
<h2 id="Description">描述</h2>
<p>静态函数<code>Promise.reject</code>返回一个被拒绝的<code>Promise对象</code>。通过使用{{jsxref("Error")}}的实例获取错误原因<code>reason</code>对调试和选择性错误捕捉很有帮助。</p>
<h2 id="示例">示例</h2>
<h3 id="使用静态Promise.reject方法">使用静态<code>Promise.reject()</code>方法</h3>
<pre><code>Promise.reject(new Error('fail')).then(function() {
// not called
}, function(error) {
console.error(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>{{SpecName('ES2015', '#sec-promise.reject', 'Promise.reject')}}</td>
<td>{{Spec2('ES2015')}}</td>
<td>ECMA规范的首次定义</td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-promise.reject', 'Promise.reject')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p>{{Compat("javascript.builtins.Promise.reject")}}</p>
<h2 id="参见">参见</h2>
<ul>
<li>{{jsxref("Promise")}}</li>
</ul>
|