-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
109 lines (77 loc) · 2.03 KB
/
main.js
File metadata and controls
109 lines (77 loc) · 2.03 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const ejemplo = 'Hola mundo';
let nombre = 'Sebastian';
// 1. Promesa
/* function cambiarNombre(){
return new Promise((resolve,reject)=>{
setTimeout(() => {
nombre = "Raul";
resolve(true);
}, 3000);
})
}
cambiarNombre().then(()=>{
})
console.log(nombre); */
// 2. todo es una clase
class Personaje {
datosPersonaje = {};
peliculasPersonaje = [];
peliculasString = '';
constructor() {
this.init();
}
async init() {
//1. traiga la informacion del personaje
await this.traerPersonaje();
console.log(this.datosPersonaje);
//2. traiga las peliculas del personaje
for (const urlPelicula of this.datosPersonaje.films) {
const nombrePelicula = await this.traerPelicula(urlPelicula);
this.peliculasPersonaje.push(nombrePelicula);
this.peliculasString += `${nombrePelicula}, `;
}
//3. imprima la infomrcion
this.imprimirInformacion();
}
traerPersonaje() {
return new Promise(resolve => {
fetch('https://swapi.dev/api/people/1')
.then(result => result.json())
.then(info => {
this.datosPersonaje = info;
resolve(true);
});
});
}
traerPelicula(url) {
return new Promise(resolve => {
fetch(url)
.then(result => result.json())
.then(info => {
resolve(info.title);
});
});
}
imprimirInformacion() {
document.getElementById('nombre').innerHTML= this.datosPersonaje.name;
document.getElementById('edad').innerHTML= this.datosPersonaje.birth_year;
document.getElementById('peliculas').innerHTML= this.peliculasString;
}
}
const personaje = new Personaje();
// Funcion fetch
/* obtenerPelicula = ()=>{
return new Promise((resolve,reject)=>{
fetch('https://swapi.dev/api/films/1')
.then(result => result.json())
.then(info => {
resolve(info);
});
})
}
async function init() {
const pelicula = await obtenerPelicula();
console.log(pelicula);
}
init();
*/