Skip to content

Instantly share code, notes, and snippets.

@jerlyrosa
Created November 2, 2020 01:23
Show Gist options
  • Save jerlyrosa/5fa3d6775d1a52bc4a6a10c3111eb79e to your computer and use it in GitHub Desktop.
Save jerlyrosa/5fa3d6775d1a52bc4a6a10c3111eb79e to your computer and use it in GitHub Desktop.
Funciones asincrona en javascript
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