The evolution of asynchronous javascript

155
THE EVOLUTION OF ASYNCHRONOUS JAVASCRIPT @cirpo

Transcript of The evolution of asynchronous javascript

Page 1: The evolution of asynchronous javascript

THE EVOLUTION OF ASYNCHRONOUS

JAVASCRIPT

@cirpo

Page 2: The evolution of asynchronous javascript

now & later

ASYNCRONY

Page 3: The evolution of asynchronous javascript

the core of asynchronous programming is the relationship between

the now and later parts of your program

ASYNCRONY

Page 4: The evolution of asynchronous javascript
Page 5: The evolution of asynchronous javascript

how to express, manage and manipulate

program behaviours

over a period of time?

Page 6: The evolution of asynchronous javascript

CONCURRENCY

Page 7: The evolution of asynchronous javascript

CONCURRENCY

a way to structure a program

by breaking it into pieces

that can be executed independently

Page 8: The evolution of asynchronous javascript

IS JS CONCURRENT?

Page 9: The evolution of asynchronous javascript

A JS runtime contains a message queue, which is a list of messages to be processed.

To each message is associated a function.

When the stack is empty, a message is taken out of the queue and processed.

QUEUE

Page 10: The evolution of asynchronous javascript

function f(b){ var a = 12;

return a+b+35; }

function g(x){ var m = 4;

return f(m*x); }

g(21);

STACK

Page 11: The evolution of asynchronous javascript

function f(b){ var a = 12;

return a+b+35; }

function g(x){ var m = 4;

return f(m*x); }

g(21);

1 foo gets called

STACK

Page 12: The evolution of asynchronous javascript

function f(b){ var a = 12;

return a+b+35; }

function g(x){ var m = 4;

return f(m*x); }

g(21);

2 first frame is created containing foo and local variable

1 foo gets called

STACK

Page 13: The evolution of asynchronous javascript

function f(b){ var a = 12;

return a+b+35; }

function g(x){ var m = 4;

return f(m*x); }

g(21);

2 first frame is created containing foo and local variable

1 foo gets called

3 when foo calls bar, a second frame is push on top of foo, containing bar

arguments and local variables

STACK

Page 14: The evolution of asynchronous javascript

function f(b){ var a = 12;

return a+b+35; }

function g(x){ var m = 4;

return f(m*x); }

g(21);

4 when bar returns, the top frame element is popped out of the stack

(leaving only foo call frame)

STACK

Page 15: The evolution of asynchronous javascript

function f(b){ var a = 12;

return a+b+35; }

function g(x){ var m = 4;

return f(m*x); }

g(21);

5 When foo returns, the stack is empty

4 when bar returns, the top frame element is popped out of the stack

(leaving only foo call frame)

STACK

Page 16: The evolution of asynchronous javascript

EVENT LOOP

Page 17: The evolution of asynchronous javascript

JS IS NON BLOCKING

Page 18: The evolution of asynchronous javascript

events

eventloop

net

filesystem

event queue thread pool

Page 19: The evolution of asynchronous javascript

JS unlike a lot of other languages, never blocks

Handling I/O is typically performed via events and callbacks

When the application is waiting for an IndexedDB query to return or an XHR request to return, it can still process other things like user input

JS IS NON BLOCKING

Page 20: The evolution of asynchronous javascript

Node.js is great for Input/Output processing but not optimised for CPU-bound work like performing a large amount of calculations.

JS IS NON BLOCKING

Page 21: The evolution of asynchronous javascript
Page 22: The evolution of asynchronous javascript

Up until recently (ES6), JS itself has actually never had any direct notion of asynchrony

built into it

Page 23: The evolution of asynchronous javascript

JS runs inside a hosting environment (the browser/nodejs)

The event loop is handled by it

Page 24: The evolution of asynchronous javascript

IS JS CONCURRENT?

Page 25: The evolution of asynchronous javascript

goroutinesprocesses

Page 26: The evolution of asynchronous javascript

IS JS CONCURRENT?

Page 27: The evolution of asynchronous javascript

JS IS SINGLE THREAD

Page 28: The evolution of asynchronous javascript

SORT OF …

Page 29: The evolution of asynchronous javascript

BUT THE HOSTING ENVIRONMENT CAN USE

MULTI THREADFOR I/O OPERATIONS

Page 30: The evolution of asynchronous javascript

most devs new to JS have issues with the fact that later doesn’t happen strictly and immediately after now

ASYNCRONY

Page 31: The evolution of asynchronous javascript

tasks that cannot complete now are, by definition, going to complete asynchronously

and thus no blocking behaviour as it might intuitively expect or want.

ASYNCRONY

Page 32: The evolution of asynchronous javascript

ASYNCRONY

Page 33: The evolution of asynchronous javascript

ASYNC IS GREAT

Page 34: The evolution of asynchronous javascript

BUT CAN BE HARD

Page 35: The evolution of asynchronous javascript

SEQUENTIAL BRAIN

Page 36: The evolution of asynchronous javascript

SEQUENTIAL BRAIN

put aside involuntary, subconscious, automatic brain functions

we are not multitasker

Page 37: The evolution of asynchronous javascript

SEQUENTIAL BRAIN

As soon as we introduce a single continuation in the form of a callback function, we have allowed a divergence to form between how our brains work and the way the code will operate

Page 38: The evolution of asynchronous javascript
Page 39: The evolution of asynchronous javascript
Page 40: The evolution of asynchronous javascript

can we do that?

Page 41: The evolution of asynchronous javascript

yes… but we are blocking

Page 42: The evolution of asynchronous javascript

WHEN YOU BLOCK,

YOU “PULL” A VALUE

Page 43: The evolution of asynchronous javascript

CALLBACK

Page 44: The evolution of asynchronous javascript
Page 45: The evolution of asynchronous javascript
Page 46: The evolution of asynchronous javascript
Page 47: The evolution of asynchronous javascript
Page 48: The evolution of asynchronous javascript

PIRAMID OF DOOM!

Page 49: The evolution of asynchronous javascript

CALLBACK HELL!

Page 50: The evolution of asynchronous javascript

BULLSHIT

Page 51: The evolution of asynchronous javascript

HOW TO AVOID POD

Page 52: The evolution of asynchronous javascript

HOW TO AVOID POD

Page 53: The evolution of asynchronous javascript

ASYNC CALLBACK

Page 54: The evolution of asynchronous javascript

ASYNC CALLBACK

=

Page 55: The evolution of asynchronous javascript

ASYNC CALLBACK

PUSH

=

Page 56: The evolution of asynchronous javascript
Page 57: The evolution of asynchronous javascript

LOSS OF CONTROL

FLOW

Page 58: The evolution of asynchronous javascript

LOSS OF ERROR

HANDLING

Page 59: The evolution of asynchronous javascript
Page 60: The evolution of asynchronous javascript

INVERSION

OF

CONTROL

Page 61: The evolution of asynchronous javascript

“Don't call us, we'll call you”

Page 62: The evolution of asynchronous javascript

INVERSION OF CONTROL

Page 63: The evolution of asynchronous javascript

INVERSION OF CONTROL

what if it’s a third party call not under our control?

Page 64: The evolution of asynchronous javascript

INVERSION OF CONTROL

Page 65: The evolution of asynchronous javascript

INVERSION OF CONTROL

what if it’s never called?

Page 66: The evolution of asynchronous javascript

INVERSION OF CONTROL

what if it’s never called?

what if it’s called too early?

Page 67: The evolution of asynchronous javascript

INVERSION OF CONTROL

what if it’s never called?

what if it’s called too early?

what if it’s called too late?

Page 68: The evolution of asynchronous javascript
Page 69: The evolution of asynchronous javascript

meh.

Page 70: The evolution of asynchronous javascript

HOW CAN YOU TELL IF

IT’S AN

ASYNC CALL?

Page 71: The evolution of asynchronous javascript
Page 72: The evolution of asynchronous javascript
Page 73: The evolution of asynchronous javascript

meh.

Page 74: The evolution of asynchronous javascript

Callbacks are the fundamental unit of asynchrony in JS.

But they’re not enough for the evolving landscape of async programming as JS

matures.

I <3 callbacks

Page 75: The evolution of asynchronous javascript

PROMISES

Page 76: The evolution of asynchronous javascript

PROMISES

It allows you to associate handlers to an asynchronous action's eventual success value or

failure reason.

This lets asynchronous methods return values like synchronous methods: instead of the final value,

the asynchronous method returns a promise.

A promise represents a proxy for a value not necessarily known when the promise is created.

Page 77: The evolution of asynchronous javascript

PROMISES

http://ecma-international.org/ecma-262/6.0/#sec-jobs-and-job-queues

Page 78: The evolution of asynchronous javascript

PROMISESPromises are now the official way to provide async return values in both JavaScript and the DOM.

All future async DOM APIs will use them, and many already do, so be prepared!

Page 79: The evolution of asynchronous javascript

PROMISES

pending: initial state, not fulfilled or rejected

fulfilled: the operation completed successfully

rejected: meaning that the operation failed.

Page 80: The evolution of asynchronous javascript

PROMISESalways async

returns a promise

handled once

thenable

Page 81: The evolution of asynchronous javascript

PROMISES

A promise must provide a then method to access its current or eventual value or rejection

reason

.then()

Page 82: The evolution of asynchronous javascript

PROMISES

.then(onFulfilled, onRejected)

Page 83: The evolution of asynchronous javascript

PROMISES

can return a promise

.then()

Page 84: The evolution of asynchronous javascript

PROMISES

.then()

.then()

.then()

Page 85: The evolution of asynchronous javascript

PROMISES

.catch()

Page 86: The evolution of asynchronous javascript

Talk is cheap, show me the code

Page 87: The evolution of asynchronous javascript

PROMISES

Page 88: The evolution of asynchronous javascript

PROMISES

Page 89: The evolution of asynchronous javascript

PROMISES

inversion of control

async or sync?

error handling

control flow

Page 90: The evolution of asynchronous javascript

WIN!

Page 91: The evolution of asynchronous javascript

BUT …

Page 92: The evolution of asynchronous javascript

PROMISE HELL!

Page 93: The evolution of asynchronous javascript

AVOID PROMISE HELL!

Page 94: The evolution of asynchronous javascript

AVOID PROMISE HELL!

Page 95: The evolution of asynchronous javascript

DON’T USE PROMISES

FOR CONTROL FLOW

Page 96: The evolution of asynchronous javascript
Page 97: The evolution of asynchronous javascript

YOUR CODEBASE THEN

BECOMES

PROMISE DEPENDANT

Page 98: The evolution of asynchronous javascript

USING PROMISES

EVERYWHERE

IMPACTS ON THE DESIGN

Page 99: The evolution of asynchronous javascript

TO PROMISE OR TO

CALLBACK?

Page 100: The evolution of asynchronous javascript

IF YOU HAVE A

LIBRARY, SUPPORT BOTH

Page 101: The evolution of asynchronous javascript
Page 102: The evolution of asynchronous javascript

SINGLE VALUE

Page 103: The evolution of asynchronous javascript

SINGLE RESOLUTION

BAD FOR STREAMS

Page 104: The evolution of asynchronous javascript

SINGLE RESOLUTION

Page 105: The evolution of asynchronous javascript

PERFORMANCES

Page 106: The evolution of asynchronous javascript

PERFORMANCES

Promises are slower compared to callbacks

You don’t get rid of callbacks, they just orchestrate callbacks in a trustable way

Page 107: The evolution of asynchronous javascript

PERFORMANCES

Promises are slower compared to callbacks

You don’t get rid of callbacks, they just orchestrate callbacks in a trustable way99.9% of the time you

won’t feel it

Page 108: The evolution of asynchronous javascript

PROMISES

Page 109: The evolution of asynchronous javascript

WHAT IF WAITING WERE

JUST AS EASY AS

BLOCKING?

Page 110: The evolution of asynchronous javascript
Page 111: The evolution of asynchronous javascript

GENERATORS

Page 112: The evolution of asynchronous javascript

GENERATORS

A new type of function that does’t not behave with the run-to-completion behaviour

Page 113: The evolution of asynchronous javascript

GENERATORS

Page 114: The evolution of asynchronous javascript

GENERATORS1 constructing the iterator, not executing

Page 115: The evolution of asynchronous javascript

GENERATORS1 constructing the iterator, not executing

2 starts the iterator

Page 116: The evolution of asynchronous javascript

GENERATORS1 constructing the iterator, not executing

2 starts the iterator

3 pause the iterator

Page 117: The evolution of asynchronous javascript

GENERATORS1 constructing the iterator, not executing

2 starts the iterator

4 resume the iterator

3 pause the iterator

Page 118: The evolution of asynchronous javascript

GENERATORS1 constructing the iterator, not executing

2 starts the iterator

4 resume the iterator

3 pause the iterator

Page 119: The evolution of asynchronous javascript

GENERATORS

with the yield where are pausing

Page 120: The evolution of asynchronous javascript

GENERATORS

A.K.A BLOCKING!

with the yield where are pausing

Page 121: The evolution of asynchronous javascript
Page 122: The evolution of asynchronous javascript

GENERATORS

Page 123: The evolution of asynchronous javascript

GENERATORS

iterator is just one side…

Page 124: The evolution of asynchronous javascript

GENERATORS

the other side is an observable

Page 125: The evolution of asynchronous javascript

GENERATORS

Page 126: The evolution of asynchronous javascript

GENERATORS

Page 127: The evolution of asynchronous javascript

GENERATORS

Page 128: The evolution of asynchronous javascript

GENERATORS

Page 129: The evolution of asynchronous javascript

GENERATORS

Page 130: The evolution of asynchronous javascript

we can block!

we can pull values

we can push values

GENERATORS

Page 131: The evolution of asynchronous javascript

GENERATORS

+

PROMISES

Page 132: The evolution of asynchronous javascript

the iterator should listen for the promise to resolve

then either resume the generator with the fulfilment message (or throw an error into the generator with the rejection reason)

GENERATORS + PROMISES

Page 133: The evolution of asynchronous javascript

GENERATORS + PROMISES

Page 134: The evolution of asynchronous javascript

GENERATORS + PROMISES

Page 135: The evolution of asynchronous javascript

GENERATORS + PROMISES

Page 136: The evolution of asynchronous javascript

GENERATORS + PROMISES

npm install co

Page 137: The evolution of asynchronous javascript
Page 138: The evolution of asynchronous javascript

BUT…

Page 139: The evolution of asynchronous javascript

ES7ES2016

to the rescue!

Page 140: The evolution of asynchronous javascript

async/await

Page 141: The evolution of asynchronous javascript

async/await

Page 142: The evolution of asynchronous javascript
Page 143: The evolution of asynchronous javascript
Page 144: The evolution of asynchronous javascript
Page 145: The evolution of asynchronous javascript

npm install -g babel-cli

babel a.es6 -o a.js

node a.js

//add it either to .babelrc or package.json{ "plugins": ["transform-async-to-generator"]}

npm install babel-plugin-transform-async-to-generator

Page 146: The evolution of asynchronous javascript

STREAMS

Page 147: The evolution of asynchronous javascript

callbacks

async/await

Page 148: The evolution of asynchronous javascript

CHOOSE YOUR CONCURRENCY MODEL

Page 149: The evolution of asynchronous javascript

A big thanks toKyle Simpson (@getify)

github.com/getify/You-Dont-Know-JS

Page 150: The evolution of asynchronous javascript

@federicogalassi

@unlucio

… and my mentors

slideshare.net/fgalassi

slideshare.net/unlucio

Page 151: The evolution of asynchronous javascript

@loige

@mariocasciaro

youtube.com/watch?v=lil4YCCXRYc

Page 152: The evolution of asynchronous javascript

@cirpo

Dev lead at

github.com/cirpo

http://sainsburys.jobs

Page 153: The evolution of asynchronous javascript
Page 154: The evolution of asynchronous javascript
Page 155: The evolution of asynchronous javascript

THANK YOU!