Skip to content

Instantly share code, notes, and snippets.

@iHani
Last active August 4, 2018 22:52
Show Gist options
  • Save iHani/867dcc29c993aafea04cb1563b757189 to your computer and use it in GitHub Desktop.
Save iHani/867dcc29c993aafea04cb1563b757189 to your computer and use it in GitHub Desktop.
async await basic example.
const isLargerOrEqualTo100 = (a) => {
return new Promise((resolve, reject) => {
if (a >= 100) {
resolve(`${a} is >= 100`)
} else {
reject(`${a} is < 100`)
}
})
}
const howLargeIs = async (number) => {
// await is used ONLY inside an async functions
// await used with functions that returns Promises
// The resolved value will be used from the called promise'd-function
return await isLargerOrEqualTo100(number)
}
howLargeIs(88).then((status) => {
console.log('Resolved: ', status)
}).catch(e => {
console.log('Rejected: ', e)
})
// Rejected: 88 is < 100
howLargeIs(200).then((status) => {
console.log('Resolved: ', status)
}).catch(e => {
console.log('Rejected: ', e)
})
// Resolved: 200 is >= 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment