Last active
August 4, 2018 22:52
-
-
Save iHani/867dcc29c993aafea04cb1563b757189 to your computer and use it in GitHub Desktop.
async await basic example.
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
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