70
syncing async kyle simpson @getify http://getify.me

Syncing Async

  • Upload
    fitc

  • View
    1.829

  • Download
    5

Embed Size (px)

DESCRIPTION

Syncing Async with Kyle Simpson Presented on September 17 2014 at FITC's Web Unleashed Toronto 2014 I bet you think “callback hell” is about function nesting and levels of indentation. Not so much. I bet you’ve heard that Promises replace callbacks. Nope. We need some clarity on what async flow control is all about in JavaScript. We’ll explore the perils of “Inversion of Control”, and see just what hell that leads us to. Then we’ll talk about promises as “continuation events” for function calls, and abstractions on top of promises that clean up our code. Finally, we’ll see how generators give us synchronous-looking async, and even how we can create cooperative concurrency with coroutines. Yield those confused async notions and control your flow. I promise we’ll get your thoughts in order. OBJECTIVE Learn why async flow control patterns are necessary for JS, why callbacks aren’t sufficient and how promises and generators are the solution. TARGET AUDIENCE JS developers ASSUMED AUDIENCE KNOWLEDGE Intermediate to advanced JS (3+ years experience). FIVE THINGS AUDIENCE MEMBERS WILL LEARN Why our brains reason about code differently than the JS engine does Why callback hell has much less to do with nesting/indentation than you thought What inversion of control is all about, and what trust issues it creates for our code What promises are and why they not only make code easier to reason about, but also solve inversion of control issues Why generators are even better, and how generators+promises are potentially the best pattern we can achieve thus far

Citation preview

Page 1: Syncing Async

syncing asynckyle simpson

@getifyhttp://getify.me

Page 2: Syncing Async

to get started...

Page 3: Syncing Async

vs.

parallel

async

Page 4: Syncing Async

threads

Page 5: Syncing Async
Page 6: Syncing Async
Page 7: Syncing Async
Page 8: Syncing Async
Page 9: Syncing Async

event-loop concurrency

Page 10: Syncing Async

1234

123

Page 11: Syncing Async

1

2

34

1

23 done!

done!

Page 12: Syncing Async

async patterns

Page 13: Syncing Async

callbacks==

continuations

Page 14: Syncing Async

callbacksgot

problems2 major^

Page 15: Syncing Async

“callback hell”

Page 16: Syncing Async

still “callback hell”

Page 17: Syncing Async

inversion of control

Page 18: Syncing Async
Page 19: Syncing Async

trust:1. not too early2. not too late3. not too many times4. not too few times5. no lost context6. no swallowed errors...

Page 20: Syncing Async
Page 21: Syncing Async

nested callbacksare not

reasonable

Page 22: Syncing Async
Page 23: Syncing Async
Page 24: Syncing Async

we write

Page 25: Syncing Async

such syncmany steps

very wow

Page 26: Syncing Async

we think

Page 27: Syncing Async

js thinks

Page 28: Syncing Async

sync-looking async

synchronoussequentialblocking

Page 29: Syncing Async

hell is callbacks

-Sartre

Page 30: Syncing Async

promise: future value

Page 31: Syncing Async

promise: “completion” event

Page 32: Syncing Async
Page 33: Syncing Async
Page 34: Syncing Async
Page 35: Syncing Async

promises:1. only resolved once2. either success OR error3. messages passed/kept4. exceptions become errors5. immutable once resolved

Page 36: Syncing Async

uninversion of control

Page 38: Syncing Async

promise: flow control

Page 39: Syncing Async
Page 40: Syncing Async

scenario:load files in parallel, render in order, but ASAP

Page 41: Syncing Async
Page 42: Syncing Async
Page 43: Syncing Async

cool story, bro.

but...

Page 44: Syncing Async
Page 45: Syncing Async
Page 46: Syncing Async

scenario:timeout of a promise

Page 47: Syncing Async
Page 49: Syncing Async

promise abstractions

Page 50: Syncing Async

sequence=

series of automatically

chained promises

Page 51: Syncing Async

github.com/getify/asynquence

async + sequence =

asynquence

Page 52: Syncing Async
Page 53: Syncing Async

remember?load files in parallel, render in order, but ASAP

Page 54: Syncing Async
Page 55: Syncing Async
Page 56: Syncing Async
Page 57: Syncing Async

promises without all the fuss

Page 58: Syncing Async

gimme moar sync

Page 59: Syncing Async

ES6 generators

davidwalsh.name/es6-generators

Page 60: Syncing Async

generator + promises

yield promises

Page 61: Syncing Async

what about pre-ES6 browsers?

Page 62: Syncing Async

facebook.github.io/regenerator/

transpiledES5

“generators”

Page 63: Syncing Async
Page 65: Syncing Async
Page 66: Syncing Async
Page 67: Syncing Async

CSP-likeconcurrency

Page 68: Syncing Async
Page 69: Syncing Async

.done()

chuckle, chuckle

Page 70: Syncing Async

thx!

kyle simpson@getify

http://getify.me