Skip to content

Instantly share code, notes, and snippets.

@jackpordi
Last active December 12, 2021 19:55
Show Gist options
  • Save jackpordi/b0599ff78e2a14b07439dd251dad464c to your computer and use it in GitHub Desktop.
Save jackpordi/b0599ff78e2a14b07439dd251dad464c to your computer and use it in GitHub Desktop.
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();
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();
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 */
}
async function asyncFunction() {
/* Returns a promise... */
}
result = await asyncFunction();
function callbackHell() {
boilPot(() => {
addCarrots();
letPotKeepBoiling(
() => {
addOnions();
letPotKeepBoiling(
() => {
console.log("Your vegetable soup is ready!");
},
1000,
);
},
5000
)
},
5000,
chopCarrots(),
chopOnions(),
);
}
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();
makeSoup();
makePasta();
function makeSoup() {
return Promise.all([
new Promise((reject, resolve) => {
chopCarrots();
chopOnions();
resolve();
}),
boilPot()
]).then(() => {
addCarrots();
return letPotKeepBoiling(5);
}).then(() => {
addOnions();
return letPotKeepBoiling(10);
})
}
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!');
}
@kdonthi
Copy link

kdonthi commented May 21, 2021

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