Introducci贸n a JavaScript
驴Por qu茅 aprender JavaScript?
隆Hola Mundo! en Mac
隆Hola Mundo! en Windows
Anatom铆a de una variable
Tipos de datos en JavaScript
Creaci贸n de strings
Operadores aritm茅ticos
Conversi贸n de tipos: Type Casting y Coerci贸n
Conversi贸n de tipos expl铆cita e impl铆cita
Estructuras de Control y L贸gica
Operadores de comparaci贸n
Operadores l贸gicos
Ejecuci贸n condicional: if
Ejercicio: Adivina el n煤mero
Ejecuci贸n condicional: switch
Loop: for
Loop: for of
Loop: for in
Loop: while
Loop: do while
Funciones y This
Anatom铆a de una funci贸n
Funciones vs M茅todos
Funciones puras e impuras
Arrow function y enlace l茅xico
Contextos de ejecuci贸n y scope chain
驴Qu茅 es Closure?
Preguntas a Desarrolladores Senior: 驴Por qu茅 aprender Desarrollo Web?
Manipulaci贸n de Arrays
Introducci贸n a Arrays
Mutabilidad e inmutabilidad de Arrays
Modificaci贸n b谩sica del final con push( ), pop( )
Iteraci贸n con map( ) y forEach( )
Filtrado y reducci贸n con filter( ) y reduce( )
B煤squeda de elementos con find( ) y findIndex( )
Crear copias con slice( )
Spread operator: casos de uso
Programaci贸n Orientada a Objetos
Anatom铆a de un Objeto
Trabajando con objetos en JavaScript
Funci贸n constructora
驴Qu茅 es una clase?
Prototipos y herencias
Herencia en la pr谩ctica
Prototipos en la pr谩ctica
this en JavaScript
Proyecto: Crea una red social
Proyecto: Crea una red social parte 2
Asincron铆a en JavaScript
驴C贸mo funciona el JavaScript Engine?
Promesas en JavaScript
Usando Async y await en JavaScript
For await of
驴C贸mo funciona la web?
驴C贸mo funciona HTTP?
M茅todo GET en JavaScript
M茅todo POST en JavaScript
M茅todo DELETE en JavaScript
Importancia del id en el m茅todo DELETE
ECMAScript 6 y tus siguientes pasos
You don't have access to this class
Keep learning! Join and start boosting your career
Have you ever faced the need to make multiple HTTP requests in a web development project? This is a common challenge, especially when interacting with multiple APIs at the same time. The most efficient solution is to use the for await...of
loop, combining async
and await
to execute promises asynchronously. This not only makes the code cleaner, but also improves performance and user experience by not blocking the loading of other elements in the application.
for await...of
and how does it work?The for await...of
is a powerful tool in JavaScript. It is a loop that allows us to iterate over asynchronous objects, such as promises, so that we can wait for each one to resolve before proceeding to the next one. The syntax is intuitive and adapts well when you need to work with multiple HTTP requests.
For example, let's say you have an array of URLs from an API, such as Rick and Morty. You can iterate over each URL and perform an asynchronous fetch request as follows:
async function fetchNewData() { const URLs = [ 'url1', // Replace with real URLs 'url2', 'url3' ];
try { for await (let URL of URLs) { let response = await fetch(URL); let data = await response.json(); console.log(data); } } } catch (error) { console.error('Error:', error); } }}
fetchNewData();
Error handling is critical when working with asynchronous requests. Using try...catch
blocks is the recommended way to catch and handle errors, such as network request failures. This ensures that your application does not fail completely if one of the requests fails and you can react appropriately, for example, by displaying an error message to the user.
Within the for await
loop, the try
block is responsible for performing the fetch
and converting the response to JSON format. If any of these operations fail, control is passed to the catch
block, where we can handle the error in an elegant way.
for await...of
in real projects?async/await
, your code becomes more readable and easier to maintain compared to traditional promises or callbacks.Implementing these techniques in your projects not only optimizes their performance, but also provides a robust foundation for handling complex scenarios where multiple data elements must be loaded and processed simultaneously. So, go ahead and apply for await...of
in your future developments and experience the improved efficiency and fluidity of your applications!
Contributions 14
Questions 3
Want to see more contributions, questions and answers from the community?