#100Devs - Fancy JS terms and Node (cohort 02)

Class 35 of our Free Web Dev Bootcamp for folx affected by the pandemic. Join live T/Th 6:30pm ET leonnoel.com/twitch and ask questions here: leonnoel.com/discord

Skip to first slide
Slide 1 of 95

 

Fancy JS terms & Node

 

Leon Noel

#100Devs

"I can't go to Outback, I got too much at stake"


Slide 2 of 95

Agenda


Slide 3 of 95

Questions

About last class or life


Slide 4 of 95

Checking In

Like and Retweet the Tweet

!checkin


Slide 5 of 95

Office Hours

SUNDAY
1:00pm EST


Slide 6 of 95

Networking

Alternatives?

1 coffee chat this week


Slide 7 of 95

USE THE SHEET!

NOW WITH TWO TABS!: Google Sheet


Slide 8 of 95

Grab The Checklist

!checklist


Slide 9 of 95

PUSH EVERY DAY


Slide 10 of 95

Submitting Work

Freelancing Submission

 


Slide 11 of 95

Submitting Work

Checklist, 100Hours Project, Codewars

 


Slide 12 of 95

Spaced Repetition

Ali Abdaal: https://youtu.be/Z-zNHHpXoMM


Slide 13 of 95

Thank You

Philip Roberts

 

Jake Archibald

Watch These Videos (Homework)


Slide 14 of 95

Backend!

Butt first!


Slide 15 of 95

Javascript is

single-threaded

Synchronous aka processes

one operation at a time

 

vs


Slide 16 of 95

If synchronous, how do we do stuff like make an api request and keep scrolling or clicking


Slide 17 of 95

Things should block


Slide 18 of 95

THE ENVIRONMENT


Slide 19 of 95

Not This


Slide 20 of 95

THIS


Slide 21 of 95

Our JS is running in

a browser

 

Browsers have a BUNCH of APIs we can use that are async and enable us to keeping looking a cute cat photos while those operations are being processed asynchronously


Slide 22 of 95

Common browser APIs

 DOM (Document Object Model) API


Slide 23 of 95

WAIT
WHAT THE FUCK

Actual words Leon said when figuring all this shit out...


Slide 24 of 95

Slide 25 of 95

So, yeah, JS can do a lot of "blocking" stuff in the browser because it is handing that stuff off to async

Web APIs


Slide 26 of 95

BUT

We are going to need to know how to handle responses coming back from those Web APIs

 

JS does this with callbacks, promises,

and eventually async/await


Slide 27 of 95

The Old School Way

Callbacks


Slide 28 of 95

You can have a function that takes another function as an argument

aka Higher Order Function


Slide 29 of 95

You have seen this a million times

addEventListener('click', callback)


Slide 30 of 95

A Callback is the function that has been passed as an argument

Callbacks are not really "a thing" in JS

just a convention 


Slide 31 of 95

Callback fires when async task or another function

 is done


Slide 32 of 95
function houseOne(){
    setTimeout(() => {
        console.log('Paper delivered to house 1')
        setTimeout(() => {
            console.log('Paper delivered to house 2')
            setTimeout(() => {
                console.log('Paper delivered to house 3')
            }, 3000)
        }, 4000)
    }, 5000)
}
houseOne()

Slide 33 of 95

Welcome To Hell

Callback Hell


Slide 34 of 95

What if there was a more readable way to handle async code


Slide 35 of 95

Promise


Slide 36 of 95

An object that MAY have a value in

the future


Slide 37 of 95

.then()

A promise object method that runs after the promise "resolves"

 


Slide 38 of 95

.then(value)

Whatever value the promise object has gets passed as an argument

 


Slide 39 of 95

We've Seen This Before


Slide 40 of 95

APIs

Fetch Fido, Fetch!

fetch("https://dog.ceo/api/breeds/image/random")
    .then(res => res.json()) // parse response as JSON
    .then(data => {
      console.log(data)
    })
    .catch(err => {
        console.log(`error ${err}`)
    });

Slide 41 of 95

Fetch returns a Promise

Like a bunch of Web APIs running async code


Slide 42 of 95
function houseOne(){
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve('Paper delivered to house 1')
        }, 1000)
    })
}
function houseTwo(){
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve('Paper delivered to house 2')
        }, 5000)
    })
}
function houseThree(){
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve('Paper delivered to house 3')
        }, 2000)
    })
}
houseOne()
    .then(data => console.log(data))
    .then(houseTwo)
    .then(data => console.log(data))
    .then(houseThree)
    .then(data => console.log(data))
    .catch(err => console.log(err))

Slide 43 of 95

Chaining Don't Read Good


Slide 44 of 95

I want my asynchronous code to look sychronous


Slide 45 of 95

Async / Await


Slide 46 of 95

A way to handle async responses


Slide 47 of 95

Promises Under The Hood

Syntactic sugar on top of promises, making asynchronous code easier to write and to read afterwards


Slide 48 of 95

Await waits for an async process to complete inside an Async Function


Slide 49 of 95
function houseOne(){
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve('Paper delivered to house 1')
        }, 1000)
    })
}
function houseTwo(){
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve('Paper delivered to house 2')
        }, 5000)
    })
}
function houseThree(){
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve('Paper delivered to house 3')
        }, 2000)
    })
}
async function getPaid(){
    const houseOneWait = await houseOne()
    const houseTwoWait = await houseTwo()
    const houseThreeWait = await houseThree()
    console.log(houseOneWait)
    console.log(houseTwoWait)
    console.log(houseThreeWait)
}
getPaid()

Slide 50 of 95
async function getPaid(){
    const houseOneWait = await houseOne()
    const houseTwoWait = await houseTwo()
    const houseThreeWait = await houseThree()
    console.log(houseOneWait)
    console.log(houseTwoWait)
    console.log(houseThreeWait)
}
getPaid()

Slide 51 of 95

I Need Something Real


Slide 52 of 95

APIs

Fetch Fido, Fetch!

async function getACuteDogPhoto(){
    const res = await fetch('https://dog.ceo/api/breeds/image/random')
    const data = await res.json()
    console.log(data)
}
getACuteDogPhoto()

Slide 53 of 95

Let's Use A Web API 

setTimeout()


Slide 54 of 95

setTimeout and setInterval are not part of the Javascript specification...

 

Most environments include them...

like all browsers and Node.js


Slide 55 of 95
function houseOne(){
    console.log('Paper delivered to house 1')
}
function houseTwo(){
    setTimeout(() => console.log('Paper delivered to house 2'), 3000)
}
function houseThree(){
    console.log('Paper delivered to house 3')
}
houseOne()
houseTwo()
houseThree()

Slide 56 of 95
function houseOne(){
    console.log('Paper delivered to house 1')
}
function houseTwo(){
    setTimeout(() => console.log('Paper delivered to house 2'), 0)
}
function houseThree(){
    console.log('Paper delivered to house 3')
}
houseOne()
houseTwo()
houseThree()

Slide 57 of 95

EVENT LOOP


Slide 58 of 95

Time for some data structures


Slide 59 of 95

Queue

Like a real queue, the first element which was added to the list, will be the first element out.

This is called a FIFO (First In First Out) structure.


Slide 60 of 95
let queue = []
queue.push(2) // queue is now [2] 
queue.push(5) // queue is now [2, 5] 
let i = queue.shift() // queue is now [5] 
alert(i) // displays 2

Queue Example


Slide 61 of 95

Stack

The first pancake made, is the last pancake served.

This is called a stack.

The first element which was added to the list, will be the last one out. This is called a LIFO (Last In First Out) structure.


Slide 62 of 95
let stack = []
stack.push(2) // stack is now [2] 
stack.push(5) // stack is now [2, 5] 
let = stack.pop() // stack is now [2] 
alert(i) // displays 5

Stack Example


Slide 63 of 95

Back To Getting Got


Slide 64 of 95

JS IS RUNNING IN THE BROWSER

 

V8 Engine
(Parse Code > Runnable Commands)

 

Window Runtime (Hosting Environment)

Gives Us Access To Web APIs

Passes stuff to Libevent (Event Loop)


Slide 65 of 95

The Event Loop monitors the Callback Queue and Job Queue and decides what needs to be pushed to the Call Stack.


Slide 66 of 95

Call Stack

Web APIs

Task Queue

(Also, a job queue)

console.log('Paper delivered to house 1')

setTimeout(() => {
  console.log('Paper delivered to house 2'), 0)
}

console.log('Paper delivered to house 3')

Console

main()

Call Stack

Web APIs

Task Queue

(Also, a job queue)

console.log('Paper delivered to house 1')

setTimeout(() => {
  console.log('Paper delivered to house 2'), 0)
}

console.log('Paper delivered to house 3')

Console

main()

log('house 1')

Call Stack

Web APIs

Task Queue

(Also, a job queue)

console.log('Paper delivered to house 1')

setTimeout(() => {
  console.log('Paper delivered to house 2'), 0)
}

console.log('Paper delivered to house 3')

Console

main()

log('house 1')

Call Stack

Web APIs

Task Queue

(Also, a job queue)

console.log('Paper delivered to house 1')

setTimeout(() => {
  console.log('Paper delivered to house 2'), 0)
}

console.log('Paper delivered to house 3')

Console

main()

log('house 1')

setTimeout()

Call Stack

Web APIs

Task Queue

(Also, a job queue)

console.log('Paper delivered to house 1')

setTimeout(() => {
  console.log('Paper delivered to house 2'), 0)
}

console.log('Paper delivered to house 3')

Console

main()

log('house 1')

setTimeout()

Call Stack

Web APIs

Task Queue

(Also, a job queue)

console.log('Paper delivered to house 1')

setTimeout(() => {
  console.log('Paper delivered to house 2'), 0)
}

console.log('Paper delivered to house 3')

Console

main()

log('house 1')

setTimeout() - cb or promise...

log('house 3')

Call Stack

Web APIs

Task Queue

(Also, a job queue)

console.log('Paper delivered to house 1')

setTimeout(() => {
  console.log('Paper delivered to house 2'), 0)
}

console.log('Paper delivered to house 3')

Console

log('house 1')

setTimeout() - cb or promise...

log('house 3')

Call Stack

Web APIs

Task Queue

(Also, a job queue)

console.log('Paper delivered to house 1')

setTimeout(() => {
  console.log('Paper delivered to house 2'), 0)
}

console.log('Paper delivered to house 3')

Console

cb / promise

log('house 1')

log('house 3')

Call Stack

Web APIs

Task Queue

(Also, a job queue)

console.log('Paper delivered to house 1')

setTimeout(() => {
  console.log('Paper delivered to house 2'), 0)
}

console.log('Paper delivered to house 3')

Console

log('house 1')

log('house 3')

log('house 2')


Slide 67 of 95

Backend BABY


Slide 68 of 95

Does Javascript have access to the DOM natively (built in)?


Slide 69 of 95

Slide 70 of 95

Javascript needed Web APIs to handle async and a bunch of stuff in the Browser 


Slide 71 of 95

JS is a language that can only do what the hosting environment allows


Slide 72 of 95

What Do Servers Need?


Slide 73 of 95

Disk Access

(hardrive/ssd)

&&

Network Access

(internet, request / responses)


Slide 74 of 95

What if there was a hosting environment that allowed JS to have disk and network access

Music & Light Warning - Next Slide


Slide 75 of 95

NODE.js BABY


Slide 76 of 95

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine.


Slide 77 of 95

Slide 78 of 95

The same shit that lets you run JS in the browser can now be used to run JS on Servers, Desktops, and elsewhere


Slide 79 of 95

True Story

V8 Engine Does All The Heavy Lifting


Slide 80 of 95

Engine Vs. Compiler


Slide 81 of 95

And just like the browser's Web APIs Node come with a bunch of stuff


Slide 82 of 95

Built in Modules

(libraries or collections of functions)

 

HTTP (network access)

FS (file system access)


Slide 83 of 95

Access to millions of packages via NPM

(groupings of one or more custom modules)


Slide 84 of 95

Not Web APIs, but C/C++ APIs


Slide 85 of 95

sorry, don't remember the source


Slide 86 of 95

Install Node


Slide 87 of 95

Releases?

LTS, Current, Nightly?


Slide 88 of 95

Let's Code

Simple Node Server


Slide 89 of 95

Just

HTTP & FS

const http = require('http')
const fs = require('fs')
http.createServer((req, res) => {
  fs.readFile('demofile.html', (err, data) => {
    res.writeHead(200, {'Content-Type': 'text/html'})
    res.write(data)
    res.end()
  })
}).listen(8000)

Music & Light Warning - Next Slide


Slide 90 of 95

You are now a

Software Engineer

that can build

Fullstack Web Applications


Slide 91 of 95

Let's Look

More Complex Backend


Slide 92 of 95

Slide 93 of 95

How could we clean this up?


Slide 94 of 95

Group Work

https://live.remo.co/e/100devs-networking-night-group-0
https://live.remo.co/e/100devs-networking-night-group-0-1
If Remo does not work for you, please jump into one of our

Discord Voice Channels!


Slide 95 of 95

Homework

 

Do: Start prepping THE BANK

Do: Complete Your Professional Links

Do: Make node-backend-simple-json more readable

Do: Make a coinflip game where the randomization happens server side