Slide 1

Slide 1 text

Taming the Asynchronous Beast in JavaScript Vasanth Krishnamoorthy

Slide 2

Slide 2 text

We’ll look into.. •  Event Loop •  Callbacks •  Promises (ES6) •  Generators (ES6) •  Async funcHons (ES7)

Slide 3

Slide 3 text

Event Loop basics •  JavaScript runHme is single threaded One thread === Single call stack === One thing at a Hme

Slide 4

Slide 4 text

Callbacks •  In JavaScript - FuncHons are 1st class objects –  FuncHons can be stored in variables, passed as arguments to funcHons, created within funcHons and returned from funcHons Because funcHons are first-class objects, we can pass a funcHon as an argument in another funcHon and later execute that passed-in funcHon.

Slide 5

Slide 5 text

Callback hell Callbacks === conHnuaHons Kinda like IncepHon….

Slide 6

Slide 6 text

Inspired by callback hell

Slide 7

Slide 7 text

Callback Hell - FlaXening

Slide 8

Slide 8 text

Callbacks -the real problem Inversion of control

Slide 9

Slide 9 text

Promises to the rescue

Slide 10

Slide 10 text

Promises A proxy for a value that will eventually become available

Slide 11

Slide 11 text

Promises - Syntax Promises can be chained, that is to say - you can save a reference to any point in the promise chain and then tack more promises on top of it. This is one of the fundamental points to understanding promises.

Slide 12

Slide 12 text

Promises Un-inversion of control

Slide 13

Slide 13 text

Promises – SeXling it •  Promises can exist in three states: pending, fulfilled, and rejected. •  Pending is the default state. From there, a promise can be “se4led” into either fulfillment or rejec9on. •  Promise.all(p1, p2, p3) –  SeXle with a single rejecHon reason as soon as one of its dependencies is rejected. –  SeXle with all fulfillment results as soon as all of its dependencies are fulfilled. •  Promise.race(p1, p2, p3) –  Promise.race() is similar to Promise.all, except the first promise to seXle will “win” the race, and its value will be passed along to branches of the race. –  RejecHons will also finish the race, and the race promise will be rejected.

Slide 14

Slide 14 text

Generators (ES6) WriHng Async code in a Sync fashion… Almost there…

Slide 15

Slide 15 text

Generators (ES6) •  Generators are func-ons which can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances. •  Helps us to handle asynchronous funcHon calls, just like it happened synchronously. •  Two things disHnguish genFunc from a normal funcHon declaraHon: –  It starts with the “keyword” funcHon*. –  It is paused in the middle via yield.

Slide 16

Slide 16 text

Generators - Syntax

Slide 17

Slide 17 text

Generators Async

Slide 18

Slide 18 text

Generator - Async •  Async code (with callbacks)

Slide 19

Slide 19 text

Generator - Async •  Async code (with generators)

Slide 20

Slide 20 text

Async FuncHons (ES7) Synchronizing Async code

Slide 21

Slide 21 text

Async FuncHons (ES7) •  Async funcHons take the idea of using generators for asynchronous programming and give them their own simple and semanHc syntax. - Removes boilerplate code mulHple returns as in promises - async fns implicitly return a promise that resolves to val - ES7 Stage 3 Drad (Babelify and all good to go)

Slide 22

Slide 22 text

Async FuncHons - Chaining

Slide 23

Slide 23 text

Async FuncHons – Handling Errors Async funcHons sHll uHlize built- in promises/generators, but they do so under the hood.

Slide 24

Slide 24 text

QuesHons? Thanks to… Kyle Simpson @geHfy - Author, You Don’t Know JS Jafar Husain @jhusain - Tech Lead, NeFlix Axel Rauschmayer @rauschma - 2ality blog For more code samples, check my repo: hXps://github.com/vasanthk/async-javascript