blob: eef15faf9acfe385b6d72b6515ace73c52585bd3 (
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
---
title: Promise.prototype.finally()
slug: Web/JavaScript/Reference/Global_Objects/Promise/finally
translation_of: Web/JavaScript/Reference/Global_Objects/Promise/finally
---
<div>{{JSRef}}</div>
<p><code><strong>finally()</strong></code> 方法會回傳一個 {{jsxref("Promise")}}。當 promise 被 settled 後,無論其結果是 fulfilled 還是 rejected ,都會執行指定的回呼函數。它提供了一個讓 <code>Promise</code> 在被確認後,無論是 fulfilled 或是 rejected 都會執行某些程式碼的一種手段。</p>
<p>這樣可以避免你在 promise 的 {{jsxref("Promise.then", "then()")}} 和 {{jsxref("Promise.catch", "catch()")}} 重複處理相同的程式碼。</p>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox"><var>p.finally(onFinally)</var>;
p.finally(function() {
// settled(fulfilled 或 rejected)
});
</pre>
<h3 id="Parameters">Parameters</h3>
<dl>
<dt><code>onFinally</code></dt>
<dd>當 <code>Promise</code> settled 後呼叫的 {{jsxref("Function")}}。</dd>
</dl>
<h3 id="Return_value">Return value</h3>
<p>回傳 {{jsxref("Promise")}} 當 <code>finally</code> 的處理函數 <code>onFinally</code> 被指定時。</p>
<h2 id="Description">Description</h2>
<p>當你希望在 promise settled 後且不關心它的結果為何時,執行一些處理或清理的工作, <code>finally()</code> 方法會很有幫助。</p>
<p><code>finally()</code> 方法非常類似於 <code>.then(onFinally, onFinally)</code> 的呼叫方式,但仍有一些差異:</p>
<ul>
<li><font><font>當建立行內的函數時,可以只傳遞一次,從而避免重複宣告或為它宣告變數。</font></font></li>
<li><code>finally</code> 的回呼函數並不會接收到任何引數,因其沒有可靠的方式來確認 promise 是被 fulfilled 還是 rejected 。它的使用情境僅適用於當你<em>不關心</em> rejection 的原因或 fulfillment 的值,因此無須提供。範例:
<ul>
<li><font><font>與 </font></font><code>Promise.resolve(2).then(() => {}, () => {})</code><font><font>(將被 resolved 為</font></font><code>undefined</code><font><font>)不同,</font></font><code>Promise.resolve(2).finally(() => {})</code><font><font> 將被 resolved 為</font></font><code>2</code><font><font>。</font></font></li>
<li><font><font>同樣的,與 </font></font><code>Promise.reject(3).then(() => {}, () => {})</code><font><font>(將 fulfilled 為</font></font><code>undefined</code><font><font>)不同,</font></font><code>Promise.reject(3).finally(() => {})</code><font><font> 將被 rejected 為</font></font><code>3</code><font><font>。</font></font></li>
</ul>
</li>
</ul>
<div class="note">
<p><strong>備註: </strong>在 finally 回呼中使用 throw (或回傳 rejected promise)會導致新的 promise 被 reject , reject 的原因則是呼叫 throw() 時所指定的值。</p>
</div>
<h2 id="Examples">Examples</h2>
<pre class="brush: js">let isLoading = true;
fetch(myRequest).then(function(response) {
var contentType = response.headers.get("content-type");
if(contentType && contentType.includes("application/json")) {
return response.json();
}
throw new TypeError("Oops, we haven't got JSON!");
})
.then(function(json) { /* process your JSON further */ })
.catch(function(error) { console.log(error); })
.finally(function() { isLoading = false; });
</pre>
<div class="hidden">
<p>Please do not add polyfills on MDN pages. For more details, refer to: <a href="https://discourse.mozilla.org/t/mdn-rfc-001-mdn-wiki-pages-shouldnt-be-a-distributor-of-polyfills/24500">https://discourse.mozilla.org/t/mdn-rfc-001-mdn-wiki-pages-shouldnt-be-a-distributor-of-polyfills/24500</a></p>
</div>
<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('ESDraft', '#sec-promise.prototype.finally', 'Promise.prototype.finally')}}</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.finally")}}</p>
<h2 id="See_also">See also</h2>
<ul>
<li>{{jsxref("Promise")}}</li>
<li>{{jsxref("Promise.prototype.then()")}}</li>
<li>{{jsxref("Promise.prototype.catch()")}}</li>
</ul>
|