Skip to content

Instantly share code, notes, and snippets.

@alexandrutapirdea
Created April 29, 2021 16:10
Show Gist options
  • Save alexandrutapirdea/f044c6c862a848ffdf44b23a762ca5c7 to your computer and use it in GitHub Desktop.
Save alexandrutapirdea/f044c6c862a848ffdf44b23a762ca5c7 to your computer and use it in GitHub Desktop.
ES6 async await
const sonChecksWeather = new Promise(function (resolve, reject) {
setTimeout(() => {
const possibleOutcome = ['Sunny', 'Rainy', 'Unexpected error']
const randomNumber = Math.floor(Math.random() * 2)
const result = possibleOutcome[randomNumber]
console.log('Son: The weather is ', result)
// Both rainy or sunny will be on resolve
if (result === 'Sunny' || result === 'Rainy') {
resolve(result)
} else {
reject(new Error('I cannot tell how the weather will be'))
}
}, 200)
})
const fatherWillDecide = async () => {
const weather = await sonChecksWeather();
if (weather === 'Sunny') {
console.log('We will go boating')
} else if (weather === 'Rainy') {
console.log('We will stay home and play boardgames')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment