-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09.js
More file actions
47 lines (39 loc) · 1.5 KB
/
09.js
File metadata and controls
47 lines (39 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Manejo de errores en fetch
fetch("https://api-que-no-existe.com/datos")
.then(response => response.json())
.then(products => console.log(products))
.catch(error => console.error("Error:", error.message));
// Dónde cae el error importa
fetch("https://excuser-three.vercel.app/v1/excuse/developers/")
.then(response => response.json())
.then(data => {
console.log("Excusa:", data[0].excuse);
throw new Error("fallo tras obtener la excusa"); // 👈 error forzado
})
.then(() => console.log("Este .then() se SALTA"))
.catch(error => {
console.error("Capturado:", error.message);
return "La cadena continúa después del catch";
})
.then(valor => console.log("Recuperado:", valor));
// Ejemplo de Finally
fetch("https://excuser-three.vercel.app/v1/excuse/developers1/")
.then(respuesta => respuesta.json())
.then(data => console.log("Excusa:", data[0].excuse))
.catch(error => console.error("Error:", error.message))
.finally(() => {
cargando = false; // Siempre se ejecuta
console.log("Cargando:", cargando); // false — tanto si hubo error como si no
});
// Cadena completa con buenas prácticas
function cargarExcusa() {
mostrarSpinner();
fetch("https://excuser-three.vercel.app/v1/excuse/developers/")
.then(res => {
if (!res.ok) throw new Error(`Error ${res.status}`);
return res.json();
})
.then(data => renderizarExcusa(data[0].excuse))
.catch(error => mostrarMensajeError(error.message))
.finally(() => ocultarSpinner());
}