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!'); | |
} |
let k = 0;
/* for loops */
k = Math.pow(k, i * j) / k;
Since k=0
, it means dividing from 0
in the first place, the calculation gives Infinity
... and then 0
or NaN
in other cases...
Does that have any particular purpose? 🤔
Forgive me but I am a Python user going to come in here and ask some questions:
import time
import asyncio
async def heatThePan():
print("Heating the pan for 2 seconds")
await time.sleep(2)
return
def heatThePanNotAsync():
print("Heating the pan for 2 seconds")
time.sleep(2)
return
def cookEggs(numEggs: int):
for i in range(numEggs):
print("Cooking Egg # {0}".format(i))
time.sleep(1)
def cookBreakfast():
begin = time.time()
heatThePan()
cookEggs(2)
end = time.time()
return (end - begin)
def cookBreakfastSlowly():
begin = time.time()
heatThePanNotAsync()
cookEggs(2)
end = time.time()
return (end - begin)
print("Slow way: {0}, Hopefully faster way: {1}".format(cookBreakfastSlowly(), asyncio.run(cookBreakfast())))
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?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@katerlouis
setTimeout
is actually ancient and isn't a promise, therefore you can't useawait
on it. It's why a lot of the my functions had to return a Promise of setTimeout instead.You're right, there's no need to give the function signature the
async
flag anymore. However, I'd consider it good practice because its much more convenient to look at the function signature to see that it returns a Promise for sure, rather than to have to look into the actual implementation, especially if its a big function. It's also nicer in my opinion to have every function that handles Promises (or async) be flagged as such for consistency.Yeap, you're right xD