-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path00_Fetch.js
More file actions
79 lines (62 loc) · 2.01 KB
/
Copy path00_Fetch.js
File metadata and controls
79 lines (62 loc) · 2.01 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
'use strict'
//FETCH para hacer peticiones asincronas al servidor/api rest
//fetch('https://jsonplaceholder.typicode.com/users')
let usuarios = document.querySelector('#users');
let janete = document.querySelector('#Janet');
let profesor = document.querySelector('#prof');
getUsuarios()
.then(data => data.json())
.then(usuario =>{
ListadoUsuarios(usuario.data);
return getInfo();
})
.then(data=>{
profesor.innerHTML = data;
return getJanet();
})
.then(data => data.json())
.then(usuaria => {
mostrarJanet(usuaria.data);
})
.catch(error=>{
console.log(error);
});
function getUsuarios(){
return fetch('https://reqres.in/api/users')
}
function getJanet(){
return fetch('https://reqres.in/api/users/2')
}
function getInfo(){
let Profesor = {
name : 'Argus',
lastname : 'Aphocraphex Ether',
url : 'https://ethereum.org/en/'
}
return new Promise((resolve , reject)=>{
let profesor_string = "";
setTimeout(()=>{
profesor_string = JSON.stringify(Profesor);
if(typeof profesor_string != 'string' || profesor_string === " ") return reject('error');
return resolve(profesor_string);
},1000);
});
}
function ListadoUsuarios(users){
users.map((usuarioactual,i)=>{
let nombre = document.createElement('h4');
nombre.innerHTML = i + usuarioactual.first_name + " " + usuarioactual.last_name;
usuarios.appendChild(nombre);
});
document.querySelector('.loading').style.display = "none";
}
function mostrarJanet(jan){
let nombre = document.createElement('h4');
let avatar = document.createElement('img');
nombre.innerHTML = jan.first_name + " " + jan.last_name;
avatar.src = jan.avatar;
avatar.width = '100';
janete.appendChild(avatar);
janete.appendChild(nombre);
document.querySelector('#Janet .loading').style.display = "none";
}