Last active
February 17, 2021 00:20
-
-
Save fegvilela/035d6fc00cec4aed575effc0e92c8a97 to your computer and use it in GitHub Desktop.
a way around async behaviour on js
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
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