diff options
| author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
|---|---|---|
| committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
| commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
| tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/promise/finally | |
| parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
| download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip | |
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/promise/finally')
| -rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/promise/finally/index.html | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/promise/finally/index.html b/files/zh-cn/web/javascript/reference/global_objects/promise/finally/index.html new file mode 100644 index 0000000000..6f0711e765 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/promise/finally/index.html @@ -0,0 +1,101 @@ +--- +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></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(() => {}, () => {})</code> (resolved的结果为<code>undefined</code>)不同,<code>Promise.resolve(2).finally(() => {})</code> resolved的结果为 <code>2</code>。</li> + <li>同样,<code>Promise.reject(3).then(() => {}, () => {})</code> (resolved 的结果为<code>undefined</code>), <code>Promise.reject(3).finally(() => {})</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 && 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 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="参见">参见</h2> + +<ul> + <li>{{jsxref("Promise")}}</li> + <li>{{jsxref("Promise.prototype.then()")}}</li> + <li>{{jsxref("Promise.prototype.catch()")}}</li> +</ul> |
