Created
November 2, 2020 01:23
-
-
Save jerlyrosa/5fa3d6775d1a52bc4a6a10c3111eb79e to your computer and use it in GitHub Desktop.
Funciones asincrona en javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cuadradoPromise =(value)=>{ | |
return new Promise((resolve,reject)=>{ | |
if(typeof value !== 'number') { | |
return reject(`El valor ingresado ${value} no es un numero es un ${typeof value}`) | |
} | |
setTimeout(() => { | |
resolve({ | |
value, | |
resul: value * 2 | |
}); | |
}, 0 || Math.random() * 1000 ); | |
}); | |
} | |
const funcionExpresadaAsync = async () =>{ | |
try { | |
console.info('Incio de la funcion Async'); | |
let obj = await cuadradoPromise(2); | |
console.log(`Async Fuction: ${obj.resul}`); | |
obj = await cuadradoPromise(4); | |
console.log(`Async Fuction: ${obj.resul}`); | |
obj = await cuadradoPromise('j'); | |
console.log(`Async Fuction ${obj.resul}`); | |
} catch (err) { | |
console.error(err); | |
} | |
} | |
funcionExpresadaAsync(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment