-
-
Save alexandrutapirdea/f044c6c862a848ffdf44b23a762ca5c7 to your computer and use it in GitHub Desktop.
ES6 async await
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 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