blob: 0c41a375093ad258de23040d10d9c97bf7b0abc2 (
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: Promise.reject()
slug: Web/JavaScript/Reference/Global_Objects/Promise/reject
translation_of: Web/JavaScript/Reference/Global_Objects/Promise/reject
---
<div>{{JSRef}}</div>
<p><code><strong>Promise.reject(reason)</strong></code><strong> </strong>方法回傳一個以 <code>reason</code> 拒絕的 <code>Promise</code> 物件。</p>
<h2 id="語法">語法</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>
<p>一個以 <code>reason</code> 拒絕的 {{jsxref("Promise")}}。</p>
<h2 id="描述">描述</h2>
<p>靜態函式 <code>Promise.reject</code> 回傳一個被拒絕的 <code>Promise。由於除錯目的及選擇性錯誤捕捉(selective error catching),使</code>用一個 <code>instanceof</code> {{jsxref("Error")}} 作為 reason 是很有幫助的。</p>
<h2 id="範例">範例</h2>
<h3 id="使用靜態方法_Promise.reject()">使用靜態方法 Promise.reject()</h3>
<pre class="brush: js">Promise.reject(new Error('fail')).then(function(error) {
// not called
}, function(error) {
console.log(error); // Stacktrace
});</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('ES2015', '#sec-promise.reject', 'Promise.reject')}}</td>
<td>{{Spec2('ES2015')}}</td>
<td>Initial definition in an ECMA standard.</td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-promise.reject', 'Promise.reject')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p class="hidden">To contribute to this compatibility data, please write a pull request against this repository: <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a>.</p>
<p>{{Compat("javascript.builtins.Promise.reject")}}</p>
<h2 id="See_also">See also</h2>
<ul>
<li>{{jsxref("Promise")}}</li>
<li><a href="https://github.com/petkaantonov/bluebird#error-handling">Selective error catching using the BlueBird Promise library</a></li>
</ul>
|