Skip to content

This is a repository for 30 different projects written in JavaScript based on WesBos's course: #JavaScript30

Notifications You must be signed in to change notification settings

quangcap/javascript30

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

78 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This is a repository for 30 different projects written in JavaScript based on WesBos's course: #JavaScript30. You can find my result here.

Content

  1. Projects List
  2. Key Concept
  3. Progress
  4. References

Project List

Key Concept

Callback function

Definition

A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.

Example


  function greeting(name) {
    alert('Hello ' + name);
  }

  function processUserInput(callback) {
    var name = prompt('Please enter your name.');
    callback(name);
  }

  processUserInput(greeting);

Asynchronous and Synchronous

Callback Hell

Definition

This is a big issue caused by coding with complex nested callbacks. Here, each and every callback takes an argument that is a result of the previous callbacks. In this manner, The code structure looks like a pyramid, making it difficult to read and maintain. Also, if there is an error in one function, then all other functions get affected. (from GeeksforGeeks)

Let’s imagine we’re trying to make a burger. To make a burger, we need to go through the following steps:

  1. Get ingredients (we’re gonna assume it’s a beef burger)
  2. Cook the beef
  3. Get burger buns
  4. Put the cooked beef between the buns
  5. Serve the burger

If these steps are synchronous, you’ll be looking at a function that resembles this:


  const makeBurger = () => {
    const beef = getBeef();
    const patty = cookBeef(beef);
    const buns = getBuns();
    const burger = putBeefBetweenBuns(buns, beef);
    return burger;
  };

  const burger = makeBurger();
  serve(burger);

However, in our scenario, let’s say we can’t make the burger ourselves. We have to instruct a helper on the steps to make the burger. After we instruct the helper, we have to WAIT for the helper to finish before we begin the next step.

If we want to wait for something in JavaScript, we need to use a callback. To make the burger, we have to get the beef first. We can only cook the beef after we get the beef.


const makeBurger = () => {
  getBeef(function(beef) {
    // We can only cook beef after we get it.
  });
};

To cook the beef, we need to pass beef into the cookBeef function. Otherwise, there’s nothing to cook! Then, we have to wait for the beef to get cooked.

Once the beef gets cooked, we get buns.


const makeBurger = () => {
  getBeef(function(beef) {
    cookBeef(beef, function(cookedBeef) {
      getBuns(function(buns) {
        // Put patty in bun
      });
    });
  });
};

After we get the buns, we need to put the patty between the buns. This is where a burger gets formed.


const makeBurger = () => {
  getBeef(function(beef) {
    cookBeef(beef, function(cookedBeef) {
      getBuns(function(buns) {
        putBeefBetweenBuns(buns, beef, function(burger) {
            // Serve the burger
        });
      });
    });
  });
};

Finally, we can serve the burger! But we can’t return burger from makeBurger because it’s asynchronous. We need to accept a callback to serve the burger.


const makeBurger = nextStep => {
  getBeef(function (beef) {
    cookBeef(beef, function (cookedBeef) {
      getBuns(function (buns) {
        putBeefBetweenBuns(buns, beef, function(burger) {
          nextStep(burger)
        })
      })
    })
  })
}

// Make and serve the burger
makeBurger(function (burger) => {
  serve(burger)
})

References for Callback function

Promise

Prototype

fetch()

this

Event Delegation

JSON

localStorage

Progress

- Day 01 - 19/09/2020: **2** *projects*. => 02/31    => 0% |#                   | 100%
- Day 02 - 20/09/2020: **2** *projects*. => 04/31    => 0% |##                  | 100%
- Day 03 - 21/09/2020: **4** *projects*. => 08/31    => 0% |#####               | 100%
- Day 04 - 22/09/2020: **3** *projects*. => 11/31    => 0% |#######             | 100%
- Day 05 - 23/09/2020: **4** *projects*. => 15/31    => 0% |#########           | 100%
- Day 06 - 24/09/2020: **3** *projects*. => 18/31    => 0% |###########         | 100%
- Day 07 - 25/09/2020: **4** *projects*. => 22/31    => 0% |##############      | 100%
- Day 08 - 26/09/2020: **0** *projects*. => 22/31    => 0% |##############      | 100%
- Day 09 - 27/09/2020: **0** *projects*. => 22/31    => 0% |##############      | 100%
- Day 10 - 28/09/2020: **0** *projects*. => 22/31    => 0% |##############      | 100%
- Day 11 - 29/09/2020: **4** *projects*. => 26/31    => 0% |################    | 100%


References

About

This is a repository for 30 different projects written in JavaScript based on WesBos's course: #JavaScript30

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published