Skip to content

Instantly share code, notes, and snippets.

@fegvilela
Last active February 17, 2021 00:20
Show Gist options
  • Save fegvilela/035d6fc00cec4aed575effc0e92c8a97 to your computer and use it in GitHub Desktop.
Save fegvilela/035d6fc00cec4aed575effc0e92c8a97 to your computer and use it in GitHub Desktop.
a way around async behaviour on js
function returnpromise(val) {
return new Promise((resolve, reject) => {
if (val > 5) {
resolve("resolved"); // fulfilled
} else {
reject("rejected"); // rejected
}
});
}
//This is how you handle errors in await
async function apicall() {
try {
console.log(await returnpromise(5))
} catch (error) {
console.log(error)
}
}
async function apicall2() {
let data = await returnpromise(2).catch((error) => {
console.log(error)
})
}
apicall2();
apicall();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment