-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclase32.js
More file actions
44 lines (34 loc) · 1.3 KB
/
clase32.js
File metadata and controls
44 lines (34 loc) · 1.3 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
const API_URL = 'https://swapi.co/api/'
const PEOPLE_URL = 'people/:id'
//const LUKE_URL = `${API_URL}${PEOPLE_URL.replace(':id', 1)}`
const opts = { crossDomain: true }
//callback (cb o cn) un segundo parametro
function obtenerPersonaje(id, callback) {
const URL = `${API_URL}${PEOPLE_URL.replace(':id', id)}`
$.get(URL, opts, callback)
//solucionamos el caso de un error en la conexion utilizando calbacks
.fail(() => {
console.log(`Sucedio un error. No se pudo obtener el personaje ${id}`);
})
}
//MANEJO DE ERRORES CON CALBACKS
//Los request se estan haciendo en serie
//callback hell(infierno de los callback) el codigo se esta volviendo horizontal
obtenerPersonaje(1, function (character){
console.log(`Hola, yo soy ${character.name}`)
obtenerPersonaje(2, function (character) {
console.log(`Hola, yo soy ${character.name}`)
obtenerPersonaje(3, function (character) {
console.log(`Hola, yo soy ${character.name}`)
obtenerPersonaje(4, function (character) {
console.log(`Hola, yo soy ${character.name}`)
obtenerPersonaje(5, function (character) {
console.log(`Hola, yo soy ${character.name}`)
obtenerPersonaje(7, function (character){
console.log(`Hola, yo soy ${character.name}`);
})
})
})
})
})
})