Last active
December 12, 2021 19:55
-
-
Save jackpordi/b0599ff78e2a14b07439dd251dad464c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function makeSoup() { | |
const pot = boilPot(); | |
chopCarrots(); | |
chopOnions(); | |
await pot; | |
addCarrots(); | |
await letPotKeepBoiling(5); | |
addOnions(); | |
await letPotKeepBoiling(10); | |
console.log("Your vegetable soup is ready!"); | |
} | |
makeSoup(); |
This file contains hidden or 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
async function makeSoup() { | |
const pot = boilPot(); | |
chopCarrots(); | |
chopOnions(); | |
await pot; | |
addCarrots(); | |
await letPotKeepBoiling(5); | |
addOnions(); | |
await letPotKeepBoiling(10); | |
console.log("Your vegetable soup is ready!"); | |
} | |
makeSoup(); |
This file contains hidden or 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
async function letPotKeepBoiling(time) { | |
return /* A promise to let the pot keep boilng for certain time */ | |
} | |
async function boilPot() { | |
return /* A promise to let the pot boil for time */ | |
} |
This file contains hidden or 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
async function asyncFunction() { | |
/* Returns a promise... */ | |
} | |
result = await asyncFunction(); |
This file contains hidden or 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
function callbackHell() { | |
boilPot(() => { | |
addCarrots(); | |
letPotKeepBoiling( | |
() => { | |
addOnions(); | |
letPotKeepBoiling( | |
() => { | |
console.log("Your vegetable soup is ready!"); | |
}, | |
1000, | |
); | |
}, | |
5000 | |
) | |
}, | |
5000, | |
chopCarrots(), | |
chopOnions(), | |
); | |
} |
This file contains hidden or 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
function chopCarrots() { | |
let k = 0; | |
for (let i=0; i<10000; i++) { | |
for (let j=0; j<10000; j++) { | |
k = Math.pow(k, i * j) / k | |
} | |
} | |
console.log("Done chopping carrots!"); | |
} | |
function chopOnions() { | |
let k = 0; | |
for (let i=0; i<10000; i++) { | |
for (let j=0; j<10000; j++) { | |
k = Math.pow(k, i * j) / k | |
} | |
} | |
console.log("Done chopping onions!"); | |
} | |
function addOnions() { | |
console.log('Add Onions to pot!'); | |
} | |
function addCarrots() { | |
console.log('Add Carrots to pot!'); | |
} | |
async function letPotKeepBoiling(time) { | |
return new Promise((resolve) => setTimeout(() => { | |
console.log("Pot has boiled for ", time, " minutes"); | |
resolve(); | |
}, time * 100)); | |
} | |
async function boilPot() { | |
return new Promise((resolve) => setTimeout(() => { | |
console.log("Done boiling pot!"); | |
resolve(); | |
}, 5000)); | |
} | |
async function makeSoup() { | |
const pot = boilPot(); | |
chopCarrots(); | |
chopOnions(); | |
await pot; | |
addCarrots(); | |
await letPotKeepBoiling(5); | |
addOnions(); | |
await letPotKeepBoiling(10); | |
console.log("Your vegetable soup is ready!"); | |
} | |
makeSoup(); |
This file contains hidden or 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
makeSoup(); | |
makePasta(); |
This file contains hidden or 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
function makeSoup() { | |
return Promise.all([ | |
new Promise((reject, resolve) => { | |
chopCarrots(); | |
chopOnions(); | |
resolve(); | |
}), | |
boilPot() | |
]).then(() => { | |
addCarrots(); | |
return letPotKeepBoiling(5); | |
}).then(() => { | |
addOnions(); | |
return letPotKeepBoiling(10); | |
}) | |
} |
This file contains hidden or 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
function chopCarrots() { | |
/** | |
* Some long computational task here... | |
*/ | |
console.log("Done chopping carrots!"); | |
} | |
function chopOnions() { | |
/** | |
* Some long computational task here... | |
*/ | |
console.log("Done chopping onions!"); | |
} | |
function addOnions() { | |
console.log('Add Onions to pot!'); | |
} | |
function addCarrots() { | |
console.log('Add Carrots to pot!'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Forgive me but I am a Python user going to come in here and ask some questions:
I understand that async basically stops a function; however, I don't understand why my code is resulting in an error - do async functions need to only be called by async functions?