Skip to content

Instantly share code, notes, and snippets.

@faustoct1
Last active July 9, 2022 05:23
Show Gist options
  • Save faustoct1/6f9ff25c5a6a2fe4fc1e1a12c83a36e2 to your computer and use it in GitHub Desktop.
Save faustoct1/6f9ff25c5a6a2fe4fc1e1a12c83a36e2 to your computer and use it in GitHub Desktop.
Diferença promise + async/await + then/catch em 10 segundos
const execute = async (params) => {
return new Promise((resolve,reject)=>{
try{
if(params===true) throw "Erro" //força exception
resolve(params)
}catch(e){
reject(e)
}
})
}
const test = async () => {
//chama execute e retorna o resolve da promise
const r1 = await execute({name:'joker',age:16,position:'programmer specialist'})
console.log(r1)
//chama execute e cai no try-catch
try{
const r2 = await execute(true)
console.log('não devia cair aqui', r2)
}catch(e){
console.log(e)
}
//chama execute depois executa then
execute({name:'joker',age:16,position:'programmer specialist'}).then((json)=>console.log(json)).catch(e=>console.log(e))
//chama execute depois executa catch
execute(true).then((json)=>console.log(json)).catch(e=>console.log(e))
}
//executa a porra toda
(async ()=>test())()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment