-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14.js
More file actions
102 lines (87 loc) · 3.25 KB
/
14.js
File metadata and controls
102 lines (87 loc) · 3.25 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Regla base de la conversión
Promise.then(resultado => algo(resultado))
const resultado = await promesa;
algo(resultado);
// Ejemplos:
// ── Con Promesas ──────────────────────────────────────
function obtenerTitulo(postId) {
return fetch(`https://jsonplaceholder.typicode.com/posts/${postId}`)
.then(respuesta => respuesta.json())
.then(post => post.title.toUpperCase())
.catch(error => console.error(error.message));
}
// ── Con async/await ───────────────────────────────────
async function obtenerTitulo(postId) {
try {
const respuesta = await fetch(`https://jsonplaceholder.typicode.com/posts/${postId}`);
const post = await respuesta.json();
return post.title.toUpperCase();
} catch (error) {
console.error(error.message);
}
}
// Ejemplo #2
// ── Con Promesas ──────────────────────────────────────
function cargarPerfil(userId) {
return obtenerUsuario(userId)
.then(usuario => {
renderizarAvatar(usuario.foto);
return obtenerPostsDeUsuario(usuario.id);
})
.then(posts => {
renderizarPosts(posts);
return obtenerComentarios(posts[0].id);
})
.then(comentarios => renderizarComentarios(comentarios))
.catch(error => mostrarError(error.message))
.finally(() => ocultarSpinner());
}
// ── Con async/await ───────────────────────────────────
async function cargarPerfil(userId) {
try {
const usuario = await obtenerUsuario(userId);
renderizarAvatar(usuario.foto);
const posts = await obtenerPostsDeUsuario(usuario.id);
renderizarPosts(posts);
const comentarios = await obtenerComentarios(posts[0].id);
renderizarComentarios(comentarios);
} catch (error) {
mostrarError(error.message);
} finally {
ocultarSpinner();
}
}
// Ejemplo #3 con Promise.all
// ── Con Promesas ──────────────────────────────────────
function inicializarApp(userId) {
return Promise.all([
obtenerUsuario(userId),
obtenerNotificaciones(userId),
obtenerConfiguracion(),
])
.then(([usuario, notificaciones, config]) => {
aplicarConfig(config);
renderizarHeader(usuario);
mostrarNotificaciones(notificaciones);
})
.catch(error => console.error("Error al inicializar:", error.message));
}
// ── Con async/await ───────────────────────────────────
async function inicializarApp(userId) {
try {
const [usuario, notificaciones, config] = await Promise.all([
obtenerUsuario(userId),
obtenerNotificaciones(userId),
obtenerConfiguracion(),
]);
aplicarConfig(config);
renderizarHeader(usuario);
mostrarNotificaciones(notificaciones);
} catch (error) {
console.error("Error al inicializar:", error.message);
}
}
// Cuando es mejor mantener promesas .then()
const nombre = await fetch("/api/usuario/1")
.then(res => res.json())
.then(u => u.nombre);