Skip to content

Instantly share code, notes, and snippets.

View alexandrutapirdea's full-sized avatar

Alexandru Tapirdea alexandrutapirdea

  • Bucharesti
View GitHub Profile
// The son will climb the tree and check the weather
const sonChecksWeather = () => {
const possibleOutcome = ["Sunny","Rainy","Unexpected error"]
setTimeout(()=>{
const randomNumber = Math.floor(Math.random()*2);
const result = possibleOutcome[randomNumber];
console.log("Son: The weather is ", result)
// the father will decide
fatherWillDecide(result)
},2000)
@alexandrutapirdea
alexandrutapirdea / .js
Created April 29, 2021 15:59
Using Promises
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)
@alexandrutapirdea
alexandrutapirdea / .js
Created April 29, 2021 16:10
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)