├── README.md ├── const-var-let.js ├── flecha.js ├── template-string.js ├── callbacks.js ├── desestructuracion.js ├── callback-hell.js ├── promesas.js └── async-await.js /README.md: -------------------------------------------------------------------------------- 1 | # Intro de JavaScript 2 | De mi curso de Node de cero a experto 3 | 4 | [Node: de cero a experto](https://fernando-herrera.com/#/curso/node-cero-experto) -------------------------------------------------------------------------------- /const-var-let.js: -------------------------------------------------------------------------------- 1 | 2 | const nombre = 'Wolverine'; 3 | 4 | if ( true ) { 5 | const nombre = 'Magneto'; 6 | 7 | // console.log(nombre); 8 | } 9 | 10 | console.log(nombre); -------------------------------------------------------------------------------- /flecha.js: -------------------------------------------------------------------------------- 1 | 2 | 3 | // function sumar( a, b ) { 4 | // return a + b; 5 | // } 6 | 7 | const sumar = (a, b) => a + b; 8 | 9 | const saludar = () => 'Hola Mundo'; 10 | 11 | 12 | console.log( sumar( 5, 10 ) ); 13 | console.log( saludar() ); 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /template-string.js: -------------------------------------------------------------------------------- 1 | 2 | const nombre = 'Deadpool'; 3 | const real = 'Wade Winston'; 4 | 5 | const normal = nombre + ' ' + real; 6 | const template = `${ nombre } ${ real }`; 7 | 8 | console.log(normal); 9 | console.log(template); 10 | 11 | const html = ` 12 |

Hola

13 |

Mundo

14 | `; 15 | 16 | console.log(html); 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /callbacks.js: -------------------------------------------------------------------------------- 1 | 2 | // setTimeout( () => { 3 | // console.log('Hola Mundo'); 4 | // } , 1000 ); 5 | 6 | 7 | const getUsuarioByID = ( id, callback ) => { 8 | 9 | const user = { 10 | id, 11 | nombre: 'Fernando' 12 | } 13 | 14 | setTimeout( () => { 15 | callback(user) 16 | }, 1500 ) 17 | 18 | } 19 | 20 | 21 | getUsuarioByID( 10, ( usuario ) => { 22 | console.log( usuario.id ); 23 | console.log( usuario.nombre.toUpperCase() ); 24 | }); 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /desestructuracion.js: -------------------------------------------------------------------------------- 1 | 2 | 3 | const deadpool = { 4 | nombre: 'Wade', 5 | apellido: 'Winston', 6 | poder: 'Regeneración', 7 | // edad: 50, 8 | getNombre() { 9 | return `${ this.nombre } ${ this.apellido } ${ this.poder }` 10 | } 11 | } 12 | 13 | // const nombre = deadpool.nombre; 14 | // const apellido = deadpool.apellido; 15 | // const poder = deadpool.poder; 16 | 17 | function imprimeHeroe({ nombre, apellido, poder, edad = 0 }) { 18 | nombre = 'Fernando'; 19 | console.log(nombre, apellido, poder, edad ); 20 | } 21 | 22 | // imprimeHeroe( deadpool ); 23 | 24 | const heroes = ['Deadpool', 'Superman', 'Batman']; 25 | 26 | // const h1 = heroes[0]; 27 | // const h2 = heroes[1]; 28 | // const h3 = heroes[2]; 29 | const [ , , h3 ] = heroes; 30 | 31 | 32 | console.log( h3 ); 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /callback-hell.js: -------------------------------------------------------------------------------- 1 | const empleados = [ 2 | { 3 | id: 1, 4 | nombre: 'Fernando' 5 | }, 6 | { 7 | id: 2, 8 | nombre: 'Linda' 9 | }, 10 | { 11 | id: 3, 12 | nombre: 'Karen' 13 | } 14 | ]; 15 | 16 | const salarios = [ 17 | { 18 | id: 1, 19 | salario: 1000 20 | }, 21 | { 22 | id: 2, 23 | salario: 1500 24 | } 25 | ]; 26 | 27 | 28 | const getEmpleado = ( id, callback ) => { 29 | 30 | const empleado = empleados.find( e => e.id === id )?.nombre 31 | 32 | if ( empleado ) { 33 | callback( null, empleado ); 34 | } else { 35 | callback(`Empleado con id ${ id } no existe`); 36 | } 37 | } 38 | 39 | const getSalario = ( id, callback ) => { 40 | 41 | const salario = salarios.find( s => s.id === id )?.salario; 42 | 43 | if ( salario ) { 44 | callback( null, salario ); 45 | } else { 46 | callback( `No existe salario para el id ${ id }` ); 47 | } 48 | 49 | } 50 | 51 | const id = 3; 52 | 53 | getEmpleado( id, ( err, empleado ) => { 54 | 55 | if ( err ) { 56 | console.log('ERROR!'); 57 | return console.log(err); 58 | } 59 | 60 | getSalario(id, (err, salario) => { 61 | 62 | if ( err ) { 63 | return console.log(err); 64 | } 65 | 66 | console.log('El empleado:', empleado, 'tiene un salario de:', salario ) 67 | }) 68 | 69 | }) 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /promesas.js: -------------------------------------------------------------------------------- 1 | const empleados = [ 2 | { 3 | id: 1, 4 | nombre: 'Fernando' 5 | }, 6 | { 7 | id: 2, 8 | nombre: 'Linda' 9 | }, 10 | { 11 | id: 3, 12 | nombre: 'Karen' 13 | } 14 | ]; 15 | 16 | const salarios = [ 17 | { 18 | id: 1, 19 | salario: 1000 20 | }, 21 | { 22 | id: 2, 23 | salario: 1500 24 | } 25 | ]; 26 | 27 | const getEmpleado = ( id ) => { 28 | 29 | 30 | return new Promise(( resolve, reject ) => { 31 | 32 | const empleado = empleados.find( e => e.id === id )?.nombre; 33 | 34 | ( empleado ) 35 | ? resolve( empleado ) 36 | : reject( `No existe empleado con id ${ id }` ); 37 | }); 38 | } 39 | 40 | const getSalario = () => { 41 | return new Promise(( resolve, reject ) => { 42 | 43 | const salario = salarios.find( s => s.id === id )?.salario; 44 | 45 | ( salario ) 46 | ? resolve( salario ) 47 | : reject( `No existe salario con id ${ id }` ); 48 | }); 49 | } 50 | 51 | 52 | 53 | const id = 3; 54 | 55 | // getEmpleado(id) 56 | // .then( empleado => console.log( empleado ) ) 57 | // .catch( err => console.log(err) ); 58 | 59 | 60 | // getSalario(id) 61 | // .then( salario => console.log( salario ) ) 62 | // .catch( err => console.log(err) ); 63 | 64 | let nombre; 65 | 66 | getEmpleado(id) 67 | .then( empleado => { 68 | nombre = empleado; 69 | return getSalario( id ) 70 | }) 71 | .then( salario => console.log( 'El empleado:', nombre, 'tiene un salario de:', salario )) 72 | .catch( err => console.log( err ) ); 73 | 74 | 75 | -------------------------------------------------------------------------------- /async-await.js: -------------------------------------------------------------------------------- 1 | const empleados = [ 2 | { 3 | id: 1, 4 | nombre: 'Fernando' 5 | }, 6 | { 7 | id: 2, 8 | nombre: 'Linda' 9 | }, 10 | { 11 | id: 3, 12 | nombre: 'Karen' 13 | } 14 | ]; 15 | 16 | const salarios = [ 17 | { 18 | id: 1, 19 | salario: 1000 20 | }, 21 | { 22 | id: 2, 23 | salario: 1500 24 | } 25 | ]; 26 | 27 | const getEmpleado = ( id ) => { 28 | 29 | 30 | return new Promise(( resolve, reject ) => { 31 | 32 | const empleado = empleados.find( e => e.id === id )?.nombre; 33 | 34 | ( empleado ) 35 | ? resolve( empleado ) 36 | : reject( `No existe empleado con id ${ id }` ); 37 | }); 38 | } 39 | 40 | const getSalario = () => { 41 | return new Promise(( resolve, reject ) => { 42 | 43 | const salario = salarios.find( s => s.id === id )?.salario; 44 | 45 | ( salario ) 46 | ? resolve( salario ) 47 | : reject( `No existe salario con id ${ id }` ); 48 | }); 49 | } 50 | 51 | 52 | const getInfoUsuario = async( id ) => { 53 | 54 | try { 55 | const empleado = await getEmpleado(id); 56 | const salario = await getSalario(id); 57 | 58 | return `El salario del empleado: ${ empleado } es de ${ salario }`; 59 | 60 | } catch (error) { 61 | throw error; 62 | } 63 | } 64 | 65 | 66 | const id = 3; 67 | 68 | getInfoUsuario( id ) 69 | .then( msg => { 70 | console.log('TODO BIEN!') 71 | console.log(msg) 72 | }) 73 | .catch( err => { 74 | console.log('TODO MAL!') 75 | console.log( err ) 76 | }); 77 | 78 | 79 | --------------------------------------------------------------------------------