Skip to content

Instantly share code, notes, and snippets.

@carlosazaustre
Created February 28, 2023 11:20
Show Gist options
  • Save carlosazaustre/90e42490090db2e47767c0766592a2ec to your computer and use it in GitHub Desktop.
Save carlosazaustre/90e42490090db2e47767c0766592a2ec to your computer and use it in GitHub Desktop.
Asincronía en JavaScript
const data = [
{
title: "Aprendiendo JavaScript",
year: "2021",
isbn: "978-87001179623",
author: "Carlos Azaustre",
},
{
title: "Aprendiendo React",
year: "2023",
isbn: "TBD",
author: "Carlos Azaustre",
},
{
title: "Clean JavaScript",
year: "2020",
isbn: "978-8567583319",
author: "Miguel A. Gómez",
},
];
// Devuelve los datos de forma asíncrona
function getData() {
return new Promise((resolve, reject) => {
if (data.length === 0) {
reject(new Error("No hay datos"));
}
setTimeout(() => {
resolve(data);
}, 2000);
});
}
// Resolución con Promesas
getData()
.then((data) => console.log(data))
.catch((error) => console.log(error));
// Resolución con Async/Await (Top Level Await)
const books = await getData();
@CarmeloCampos
Copy link

Siuuuuu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment