Get to know MDN better
此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2018年10月.
finally() 方法會回傳一個 Promise。當 promise 被 settled 後,無論其結果是 fulfilled 還是 rejected ,都會執行指定的回呼函數。它提供了一個讓 Promise 在被確認後,無論是 fulfilled 或是 rejected 都會執行某些程式碼的一種手段。
當 Promise settled 後呼叫的 Function。
回傳 Promise 當 finally 的處理函數 onFinally 被指定時。
當你希望在 promise settled 後且不關心它的結果為何時,執行一些處理或清理的工作, finally() 方法會很有幫助。
finally() 方法非常類似於 .then(onFinally, onFinally) 的呼叫方式,但仍有一些差異:
備註:在 finally 回呼中使用 throw(或回傳 rejected promise)會導致新的 promise 被 reject,reject 的原因則是呼叫 throw() 時所指定的值。
| ECMAScript® 2027 Language Specification # sec-promise.prototype.finally |
Enable JavaScript to view this browser compatibility table.
This page was last modified on 2025年7月14日 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.