Created
February 28, 2023 11:20
-
-
Save carlosazaustre/90e42490090db2e47767c0766592a2ec to your computer and use it in GitHub Desktop.
Asincronía en JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Siuuuuu