-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
174 lines (159 loc) · 6.36 KB
/
index.html
File metadata and controls
174 lines (159 loc) · 6.36 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Promesas de JavaScript</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Hedvig+Letters+Sans&display=swap" rel="stylesheet">
<link rel="stylesheet" href="style.css">
<script src="menu.js"></script>
</head>
<body>
<nav class="navbar">
<div class="menu-toggle">
<div class="hamburger">
</div>
</div>
<ul class="nav-list">
<li class="nav-item">
<a class="nav-link active" href="#definicion">
Definición
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#sintaxis">
Sintaxis
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#estados">
Estados
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#metodos">
Métodos
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="tipos.html">
Tipos
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="ejemplos.html">
Ejemplos
</a>
</li>
</ul>
</nav>
<main>
<h1>Promesas de JavaScript</h1>
<section id="definicion">
<h2>Definición</h2>
<article>
<p>Las <strong>promesas</strong> son:</p>
<ul>
<li>Objetos que representan un valor que puede estar disponible ahora, en el futuro o nunca.
</li>
<li>Un mecanismo para manejar operaciones asincrónicas de una manera más organizada y legible.</li>
<li>Permiten realizar tareas como solicitudes de red, operaciones de archivo o cualquier operación
que tome
tiempo, sin bloquear la ejecución del programa y sin necesidad de callbacks anidadas, conocidas
como
<mark>"Callback Hell"</mark>.
</li>
</ul>
</article>
</section>
<section id="sintaxis">
<h2>Sintaxis</h2>
<article>
<div class="codigo">
<code>
let myPromise = new Promise(function(myResolve, myReject) {<br>
// "Producing Code" (May take some time)<br>
myResolve(); // when successful<br>
myReject(); // when error<br>
});<br>
// "Consuming Code" (Must wait for a fulfilled Promise)<br>
myPromise.then(<br>
function(value) { /* code if successful */ },<br>
function(error) { /* code if some error */ }<br>
);</code>
</div>
</article>
</section>
<section id="estados">
<h2>Estados de una promesa</h2>
<article>
<h3>Pendiente (Pending):</h3>
<p>El estado inicial de una Promesa antes de que se resuelva o se rechace.</p>
<h3>Resuelta (Fulfilled):</h3>
<p>La Promesa se ha resuelto con éxito y devuelve un valor.</p>
<h3>Rechazada (Rejected):</h3>
<p>La Promesa ha sido rechazada debido a un error y devuelve una razón.</p>
</article>
</section>
<section id="metodos">
<h2>Métodos de una promesa</h2>
<article>
<h3>.then(onFulfilled, onRejected):</h3>
<p>Se utiliza para adjuntar funciones de <em>callback</em> que se ejecutan <strong>cuando la Promesa se
resuelve con éxito (en el estado "Fulfilled")</strong> o <strong>cuando se rechaza (en el estado
"Rejected")</strong>.
La función <strong>onFulfilled</strong> se ejecutará con el <strong>valor resuelto</strong> y la
función </¡strong>onRejected</strong> se ejecutará con la <strong>razón del rechazo</strong>.</p>
</article>
<article>
<h3>.catch(onRejected):</h3>
<p>Un atajo para manejar solo el caso de rechazo, es equivalente a llamar a .then(null, onRejected).</p>
</article>
</section>
</main>
<!-- Footer -->
<footer>
<p>Píldora sobre JavaScript Promises, presentada por Alejandra Eng</p>
<p>Curso de Desarrollo Fullstack con Tecnologías Inmersivas de Factoría F5 y Fundación Tomillo.</p>
</footer>
<!-- JavaScript -->
<!-- Menú Toggle -->
<script>
document.addEventListener("DOMContentLoaded", function () {
const navbar = document.querySelector(".navbar");
const menuToggle = document.querySelector(".menu-toggle");
menuToggle.addEventListener("click", () => {
navbar.classList.toggle("open");
});
});
</script>
<!-- Promesa de red -->
<script>
const promesared = new Promise((resolve, reject) => {
setTimeout(() => {
const exito = false; // Cambiar a false para simular un rechazo
if (exito) {
resolve("¡Éxito! Datos recibidos");
} else {
reject("Error: No se pudieron recibir los datos");
}
}, 2000);
});
promesared
.then((resultado) => {
console.log(resultado);
console.log = function (message) {
document.getElementById("promesaredtrue").innerHTML += message
}
})
.catch((error) => {
console.error(error);
console.log = function (message) {
document.getElementById("promesaredfalse").innerHTML += message
}
});
</script>
</body>
</html>