41
@hunterloftis Stop JavaScripting like it’s 1999 modern JS @CharlotteJS

Stop JavaScripting like it's 1999

Embed Size (px)

Citation preview

@hunterloftis

Stop JavaScripting like it’s 1999 modern JS @CharlotteJS

@hunterloftis

@hunterloftis

@hunterloftis

@hunterloftis

read package.json

parse JSON

fetch repository.url

parse the body

match “X users starred” in the body

return the match

1

2

3

4

5

6

Fetching Stars

@hunterloftis

1

2

3

4

5

6

@hunterloftis

Why?

@hunterloftis

!==

blocking/slow 🐢concurrent/fast 🚗

@hunterloftis

concurrency tax

@hunterloftis

catch errors passed as arguments

catch errors from rejected promises

catch thrown errors

blocking/slow 🐢concurrent/fast 🚗

create new execution contexts

@hunterloftis

That’s just how JavaScript works.

worked

@hunterloftis

new tools for cheap concurrency

node >= 7.6.0 (Feb 22, 2017)

@hunterloftis

new tools for cheap concurrency

@hunterloftis

So? There’s always something new

in JavaScript.

@hunterloftis

Today:

1. What does modern JS look like?

2. How can you start writing it?

3. Which obstacles should you

expect?

@hunterloftis

Modern JavaScript:

never write a callback again.

@hunterloftis

concurrent/fast 🚗

instead, your code will read like Python

…and run like JavaScript!

@hunterloftis

How?

@hunterloftis

Three Rules:

@hunterloftis

1. Whenever you think about writing

a callback, use async / await

instead.

@hunterloftis

this is a callback function

@hunterloftis

2. throw and catch Errors,

or allow them to bubble.

But otherwise leave them alone!

@hunterloftis

@hunterloftis

“Pyramid of Doom” “Callback Hell”

@hunterloftis

3.Instead of

Promise method chains, use

await + simple assignment.

@hunterloftis

@hunterloftis

Is this still JavaScript?

• no more callbacks • no more `if (err)` • no more Promises

@hunterloftis

The Three Objections

@hunterloftis

1. I don’t understand async/await (which means I don’t understand Promises)

@hunterloftis

@hunterloftis

“I return a Promise.”

“I wait here for another Promise to resolve.”

?

@hunterloftis

@hunterloftis

2. Lots of modules still use callbacks (like node’s core APIs)

@hunterloftis

@hunterloftis

3. I work on a huge codebase (and rewrites are terrible)

@hunterloftis

@hunterloftis

a modern foo

@hunterloftis

async

json = await…

res = await…

body = await…

1. no callbacks!

2. throw errors!

3. no chaining!

what’s left?

@hunterloftis

concurrent/fast 🚗

@hunterloftis

Before After

@hunterloftis

Thank you!

Now come get some swag.

[email protected]