From 4b1a9203c547c019fc5398082ae19a3f3d4c3efe Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:41:15 -0500 Subject: initial commit --- .../global_objects/promise/finally/index.html | 93 ++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 files/de/web/javascript/reference/global_objects/promise/finally/index.html (limited to 'files/de/web/javascript/reference/global_objects/promise/finally/index.html') diff --git a/files/de/web/javascript/reference/global_objects/promise/finally/index.html b/files/de/web/javascript/reference/global_objects/promise/finally/index.html new file mode 100644 index 0000000000..588d00d43f --- /dev/null +++ b/files/de/web/javascript/reference/global_objects/promise/finally/index.html @@ -0,0 +1,93 @@ +--- +title: Promise.prototype.finally() +slug: Web/JavaScript/Reference/Global_Objects/Promise/finally +translation_of: Web/JavaScript/Reference/Global_Objects/Promise/finally +--- +
{{JSRef}}
+ +

finally () gibt ein {{jsxref ("Promise")}} zurück. Wenn das Promise erfüllt oder abgelehnt wird, wird die angegebene callback-Funktion ausgeführt. Dies stellt eine Möglichkeit für Code bereit, der ausgeführt werden muss, unabhängig davon, ob das Promise erfolgreich erfüllt wurde oder nicht. Auf diese Weise können Sie vermeiden, dass derselbe Code für die beiden Handlern des Promise {{jsxref ("Promise.then", "then ()")}} und {{jsxref ("Promise.catch", "catch ()")}} geschrieben werden muss.

+ +

Syntax

+ +
p.finally(onFinally);
+
+p.finally(function() {
+   // settled (fulfilled or rejected)
+});
+
+ +

Parameters

+ +
+
onFinally
+
A {{jsxref("Function")}} called when the Promise is settled.
+
+ +

Return value

+ +

Returns a {{jsxref("Promise")}} whose finally handler is set to the specified function, onFinally.

+ +

Description

+ +

The finally() method can be useful if you want to do some processing or cleanup once the promise is settled, regardless of its outcome.

+ +

The finally() method is very similar to calling .then(onFinally, onFinally) however there are couple of differences:

+ + + +
+

Note: A throw (or returning a rejected promise) in the finally callback will reject the new promise with the rejection reason specified when calling throw().

+
+ +

Examples

+ +
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; });
+
+
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
TC39 proposalStage 4
+ +

Browser compatibility

+ + + +

{{Compat("javascript.builtins.Promise.finally")}}

+ +

See also

+ + -- cgit v1.2.3-54-g00ecf