aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/promise/finally/index.html
blob: bf7a3d736a225421a23fa54e2f650bacc63e715f (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
---
title: Promise.prototype.finally()
slug: Web/JavaScript/Reference/Global_Objects/Promise/finally
tags:
  - JavaScript
  - Promises
  - Reference
  - finally
translation_of: Web/JavaScript/Reference/Global_Objects/Promise/finally
---
<div>{{JSRef}}</div>



<div><code><strong>finally()</strong></code> 方法返回一个{{jsxref("Promise")}}。在promise结束时,无论结果是fulfilled或者是rejected,都会执行指定的回调函数。这为在<code>Promise</code>是否成功完成后都需要执行的代码提供了一种方式。</div>

<div>这避免了同样的语句需要在{{jsxref("Promise.then", "then()")}}{{jsxref("Promise.catch", "catch()")}}中各写一次的情况。</div>

<h2 id="语法">语法</h2>

<pre class="syntaxbox"><var>p.finally(onFinally)</var>;

p.finally(function() {
   // 返回状态为(resolved 或 rejected)
});</pre>

<h3 id="参数">参数</h3>

<dl>
 <dt><code>onFinally</code></dt>
 <dd><code>Promise</code> 结束后调用的{{jsxref("Function")}}</dd>
</dl>

<h3 id="返回值">返回值</h3>

<p>返回一个设置了 <code>finally</code> 回调函数的{{jsxref("Promise")}}对象。 </p>

<h2 id="描述">描述</h2>

<p>如果你想在 promise 执行完毕后无论其结果怎样都做一些处理或清理时,<code>finally()</code> 方法可能是有用的。</p>

<p><code>finally()</code> 虽然与 <code>.then(onFinally, onFinally)</code> 类似,它们不同的是:</p>

<ul>
 <li>调用内联函数时,不需要多次声明该函数或为该函数创建一个变量保存它。</li>
 <li>由于无法知道<code>promise</code>的最终状态,所以<code>finally</code>的回调函数中不接收任何参数,它仅用于无论最终结果如何都要执行的情况。</li>
 <li><code>Promise.resolve(2).then(() =&gt; {}, () =&gt; {})</code> (resolved的结果为<code>undefined</code>)不同,<code>Promise.resolve(2).finally(() =&gt; {})</code> resolved的结果为 <code>2</code></li>
 <li>同样,<code>Promise.reject(3).then(() =&gt; {}, () =&gt; {})</code> (fulfilled的结果为<code>undefined</code>), <code>Promise.reject(3).finally(() =&gt; {})</code> rejected 的结果为 <code>3</code></li>
</ul>

<div class="note">
<p><strong>备注:</strong> 在<code>finally</code>回调中 <code>throw</code>(或返回被拒绝的promise)将以 <code>throw()</code> 指定的原因拒绝新的promise.</p>
</div>

<h2 id="示例">示例</h2>

<pre class="brush: js">let isLoading = true;

fetch(myRequest).then(function(response) {
    var contentType = response.headers.get("content-type");
    if(contentType &amp;&amp; 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>

<h2 id="规范">规范</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="浏览器兼容性">浏览器兼容性</h2>

<p>{{Compat("javascript.builtins.Promise.finally")}}</p>

<h2 id="参见">参见</h2>

<ul>
 <li>{{jsxref("Promise")}}</li>
 <li>{{jsxref("Promise.prototype.then()")}}</li>
 <li>{{jsxref("Promise.prototype.catch()")}}</li>
</ul>