Lily Peng

Javascript

Back to Front-End Essentials

HTML

CSS

React

The DOM

Promises

An object that may produce a single value some time in the future for if the value is not known when the promise is created.

A promise can only be in one of 3 states, and once it reaches a resolved state, it cannot be changed: pending, fulfilled, and rejected.

The promise constructor takes in a function with two function parameters, resolve(), and reject(). When it reaches either state, the message will be stored in the variable.

Every promise object has a .then() method that takes in 2 optional callback functions for the success and failure cases of the promise. Because .then() returns a new promise object, it is possible to chain promises. This allows us to mimic synchronous code with asynchronous functions.

Promises are built on top of callbacks to write sequential asynchronous code with error catching.

They're used for network requests such as fetching data from an API or in other asynchronous functions.

^ To catch errors that may even occur during the .then(), you can pass only the success parameter in .then(), then chain on a .catch() for the error

To create a timeout promise that will invoke if the other(s) are taking too long, use Promise.race():

^ This also uses IIFE (Immediately-Invoked Function Expression), which is an anonymous function that immediately invokes because of the ().

Use Promise.all() to run Promises synchronously, to return an array of their responses after they are all done

Advanced ES6+

Closure

Closed Over Variable Environment (COVE)

Creating persistent memory (variables/data) from an outer function accessible to an inner, generated function. Every function created receives data from the memory space where the function definition lives (normally global).

Memoization

A technique of storing the results of function calls and returning the cached result when it receives the same input again. Helps to optimize performance by not calling expensive functions more than once.

Built upon the concepts of closure and higher-order functions (returning functions from functions).

Event Delegation

Add event listeners to parent instead of child elements. Uses less memory (don't need one for every child) and don't need to unbind and rebind for different children.

To avoid event bubbling (events playing from bottom element to top), use: event.stopPropogation();

JS Modules

Sectioning your code into self-contained parts that are not dependent on each other, so you can make changes without disrupting everything else. Allows you to not worry as much about sharing global variable names.

Types of For Loops

Declare vs. Initialize vs. Instantiate

Regular Expressions (RegEx)

Sample Challenges