Skip to content

Instantly share code, notes, and snippets.

@jackpordi
Last active December 12, 2021 19:55

Revisions

  1. jackpordi revised this gist Sep 3, 2019. 1 changed file with 55 additions and 0 deletions.
    55 changes: 55 additions & 0 deletions complete.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    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();
  2. jackpordi revised this gist Sep 3, 2019. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions explicitPromises.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    function makeSoup() {
    return Promise.all([
    new Promise((reject, resolve) => {
    chopCarrots();
    chopOnions();
    resolve();
    }),
    boilPot()
    ]).then(() => {
    addCarrots();
    return letPotKeepBoiling(5);
    }).then(() => {
    addOnions();
    return letPotKeepBoiling(10);
    })
    }
  3. jackpordi revised this gist Sep 3, 2019. 1 changed file with 21 additions and 0 deletions.
    21 changes: 21 additions & 0 deletions callbackHell.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    function callbackHell() {
    boilPot(() => {
    addCarrots();
    letPotKeepBoiling(
    () => {
    addOnions();
    letPotKeepBoiling(
    () => {
    console.log("Your vegetable soup is ready!");
    },
    1000,
    );
    },
    5000
    )
    },
    5000,
    chopCarrots(),
    chopOnions(),
    );
    }
  4. jackpordi revised this gist Sep 3, 2019. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions cookDinner.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@

    makeSoup();
    makePasta();
  5. jackpordi revised this gist Sep 3, 2019. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions allTogetherGood.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    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();
  6. jackpordi revised this gist Sep 3, 2019. 1 changed file with 13 additions and 9 deletions.
    22 changes: 13 additions & 9 deletions allTogetherBad.js
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,13 @@
    const pot = boilPot();
    chopCarrots();
    chopOnions();
    await pot;
    addCarrots();
    await letPotKeepBoiling(5);
    addOnions();
    await letPotKeepBoiling(10);
    console.log("Your vegetable soup is ready!");
    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();
  7. jackpordi revised this gist Sep 3, 2019. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions allTogetherBad.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    const pot = boilPot();
    chopCarrots();
    chopOnions();
    await pot;
    addCarrots();
    await letPotKeepBoiling(5);
    addOnions();
    await letPotKeepBoiling(10);
    console.log("Your vegetable soup is ready!");
  8. jackpordi revised this gist Sep 3, 2019. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions awaitExample.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    async function asyncFunction() {
    /* Returns a promise... */
    }

    result = await asyncFunction();
  9. jackpordi revised this gist Sep 3, 2019. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions asyncFunctions.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    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 */
    }
  10. jackpordi revised this gist Sep 3, 2019. 2 changed files with 1 addition and 10 deletions.
    1 change: 0 additions & 1 deletion asynchronousExample.js
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    console.log('init');
    10 changes: 1 addition & 9 deletions syncFunctions.js
    Original file line number Diff line number Diff line change
    @@ -18,12 +18,4 @@ function addOnions() {

    function addCarrots() {
    console.log('Add Carrots to pot!');
    }

    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 */
    }
    }
  11. jackpordi revised this gist Sep 3, 2019. 1 changed file with 29 additions and 0 deletions.
    29 changes: 29 additions & 0 deletions syncFunctions.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    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!');
    }

    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 */
    }
  12. jackpordi created this gist Sep 3, 2019.
    1 change: 1 addition & 0 deletions asynchronousExample.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    console.log('init');