finally()
method returns a
Promise
. When the promise is settled, i.e either fulfilled or rejected, the specified callback function is executed. This provides a way for code to be run whether the promise was fulfilled successfully or rejected once the
Promise
has been dealt with.
This helps to avoid duplicating code in both the promise's
then()
and
catch()
handlers.
p.finally(onFinally);
p.finally(function() {
// settled (fulfilled or rejected)
});
onFinally
Function
called when the
Promise
is settled.
返回
Promise
whose
finally
handler is set to the specified function,
onFinally
.
finally()
method can be useful if you want to do some processing or cleanup once the promise is settled, regardless of its outcome.
finally()
method is very similar to calling
.then(onFinally, onFinally)
however there are a couple of differences:
finally
callback will not receive any argument, since there's no reliable means of determining if the promise was fulfilled or rejected. This use case is for precisely when you
do not care
about the rejection reason, or the fulfillment value, and so there's no need to provide it. So for example:
Promise.resolve(2).then(() => {}, () => {})
(which will be resolved with
undefined
),
Promise.resolve(2).finally(() => {})
will be resolved with
2
.
Promise.reject(3).then(() => {}, () => {})
(which will be fulfilled with
undefined
),
Promise.reject(3).finally(() => {})
will be rejected with
3
.
注意:
A
throw
(or returning a rejected promise) in the
finally
callback will reject the new promise with the rejection reason specified when calling
throw
.
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.error(error); /* this line can also throw, e.g. when console = {} */ })
.finally(function() { isLoading = false; });
Please do not add polyfills on MDN pages. For more details, refer to: https://discourse.mozilla.org/t/mdn-rfc-001-mdn-wiki-pages-shouldnt-be-a-distributor-of-polyfills/24500
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Promise.prototype.finally' in that specification. |
To contribute to this compatibility data, please write a pull request against this repository: https://github.com/mdn/browser-compat-data .
更新 GitHub 上的兼容性数据| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
finally()
|
Chrome 63 | Edge 18 | Firefox 58 | IE No | Opera 50 | Safari 11.1 | WebView Android 63 | Chrome Android 63 | Firefox Android 58 | Opera Android 46 | Safari iOS 11.3 | Samsung Internet Android 8.0 | nodejs 10.0.0 |
完整支持
不支持
Promise
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()