-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
33 lines (26 loc) · 1 KB
/
Copy pathscript.js
File metadata and controls
33 lines (26 loc) · 1 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
// Función asíncrona
const getPosts = async () => {
// URL guardada en una variable
const url = 'https://jsonplaceholder.typicode.com/posts';
// Bloque try/catch para conectarse a la URL con método fetch
try {
const response = await fetch(url);
// Guardar respuesta del arreglo de datos JSON e una variable
const data = await response.json();
// Guardar en la variable el elemento <ul>
let lista = document.querySelector('#post-data > ul');
// Recorrer el arreglo obtenido con forEach(), e ir agragando
// los elementos de a uno a la lista
data.forEach((elemento)=>{
lista.innerHTML += `<li>
<p><strong>${elemento.title}</strong></p>
<p>${elemento.body}</p>
</br>
</li>`;
});
}
// Método catch() para atrapar errores
catch(err) {
console.log(err);
}
}