Promesas y async/await
Ya usaste fetch en el módulo anterior. Ahora entiendes de verdad qué pasaba por dentro.
Promises and async/await
You already used fetch in an earlier module. Now you truly understand what was happening underneath.
JavaScript hace una sola cosa a la vez. Si fetch() "congelara" el programa
mientras espera la respuesta de internet, toda la página se trabaría hasta que llegue. En vez
de eso, JavaScript sigue trabajando y te avisa cuando el dato llega, con una
promesa.
JavaScript does one thing at a time. If fetch() "froze" the program while
waiting for a reply from the internet, the whole page would lock up until it arrived. Instead,
JavaScript keeps working and lets you know when the data arrives, through a
promise.
Una promesa siempre está en uno de tres estados: pendiente (todavía
esperando), cumplida (llegó bien, dispara .then()), o
rechazada (algo salió mal, dispara .catch()).
A promise is always in one of three states: pending (still waiting),
fulfilled (it arrived fine, triggers .then()), or
rejected (something went wrong, triggers .catch()).
.then() encadenado varias veces se vuelve difícil de seguir. async
y await son azúcar sintáctico: la misma promesa por dentro, pero
escrita como si fuera código normal, de arriba a abajo.
.then() chained several times gets hard to follow. async and
await are syntactic sugar: the same promise underneath, but
written as if it were regular top-to-bottom code.
Ese es exactamente el patrón que usaste con fetch en
el tutorial anterior: await fetch(...) espera la respuesta antes de seguir a la
siguiente línea, sin congelar el resto de la página.
That is exactly the pattern you used with fetch in the
previous lesson: await fetch(...) waits for the response before moving to the
next line, without freezing the rest of the page.
Con async/await, los errores se atrapan con try/catch —lo mismo
que ya viste en el tutorial de manejo de errores—, en vez de .catch().
With async/await, errors are caught with try/catch — the same
thing you already saw in the error handling lesson — instead of .catch().
Qué es una promesa y sus tres estados, cómo escribirlas con .then()/.catch(),
y por qué async/await es la forma preferida hoy en día para escribir lo mismo de
forma más clara.
Ahora, tres métodos de array que vas a usar constantemente: map, filter y reduce, a fondo.
What a promise is and its three states, how to write them with
.then()/.catch(), and why async/await is today's
preferred way to write the same thing more clearly.
Now, three array methods you will use constantly: map, filter and reduce, in depth.