From a55b575e8089ee6cab7c5c262a7e6db55d0e34d6 Mon Sep 17 00:00:00 2001 From: Florian Merz Date: Thu, 11 Feb 2021 14:46:50 +0100 Subject: unslug es: move --- .../global_objects/promise/finally/index.html | 95 ++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 files/es/web/javascript/reference/global_objects/promise/finally/index.html (limited to 'files/es/web/javascript/reference/global_objects/promise/finally/index.html') diff --git a/files/es/web/javascript/reference/global_objects/promise/finally/index.html b/files/es/web/javascript/reference/global_objects/promise/finally/index.html new file mode 100644 index 0000000000..8d21aa785a --- /dev/null +++ b/files/es/web/javascript/reference/global_objects/promise/finally/index.html @@ -0,0 +1,95 @@ +--- +title: Promise.prototype.finally() +slug: Web/JavaScript/Referencia/Objetos_globales/Promise/finally +translation_of: Web/JavaScript/Reference/Global_Objects/Promise/finally +--- +
{{JSRef}}
+ +

El método finally() devuelve una {{jsxref("Promise")}}. Cuando la promesa se resuelve, sea exitosa o rechazada, la función de callback específicada será ejecutada. Esto ofrece una forma de ejecutar código sin importar como se haya resuelto la promesa.

+ +

Esto ayuda a evitar tener código duplicado tanto en el {{jsxref("Promise.then", "then()")}} como en el {{jsxref("Promise.catch", "catch()")}}.

+ +

Sintaxis

+ +
p.finally(alFinalizar);
+
+p.finally(function() {
+   // finalizada (exitosa o rechazada)
+});
+
+ +

Parámetros

+ +
+
alFinalizar
+
Una {{jsxref("Function")}} llamada cuando la Promise se resuelve con éxito o falla.
+
+ +

Valor de retorno

+ +

Devuelve una {{jsxref("Promise")}} cuyo finally fue fijado a la función específicada en alFinalizar.

+ +

Descripción

+ +

El método finally() puede ser de utilidad si quieres hacer algún proceso o limpieza una vez que la promesa termina, sin importar su resultado.

+ +

Utilizar finally() es muy similar a llamar .then(onFinally, onFinally), sin embargo tiene algunas diferencias:

+ + + +
+

Nota: Un throw (o retornar una promesa rechazada) en el callback finally va a rechazar la nueva promesa con la razón de rechazo especificada al llamar throw().

+
+ +

Ejemplos

+ +
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, no hemos obtenido un JSON!");
+  })
+  .then(function(json) { /* procesar el JSON */ })
+  .catch(function(error) { console.log(error); /* esta línea podría arrojar error, e.g. cuando console = {} */ })
+  .finally(function() { isLoading = false; });
+
+
+ +

Especificaciones

+ + + + + + + + + + + + + + +
EspecificaciónEstadoComentario
{{SpecName('ESDraft', '#sec-promise.prototype.finally', 'Promise.prototype.finally')}}{{Spec2('ESDraft')}}
+ +

Compatibilidad en navegador

+ + + +

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

+ +

Ver también

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