One of JavaScript’s strengths is how it handles asynchronous code. Async is one of the most important and often misunderstood part of Javascript or any other language. Async is hard because we, as human beings, can’t do two conscious actions at once and think about both of them at the same moment. In this talk we will see how asynchronous JavaScript evolved over the years. It all started with callbacks… and it landed on generators!
9. 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
10. function f(b){
var a = 12;
return a+b+35;
}
function g(x){
var m = 4;
return f(m*x);
}
g(21);
STACK
11. 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
12. 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
13. 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
14. 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
15. 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
19. 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
20. 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
21.
22. Up until recently (ES6), JS itself has actually
never had any direct notion of asynchrony
built into it
23. JS runs inside a hosting environment (the
browser/nodejs)
The event loop is handled by it
30. most devs new to JS have issues with the fact
that later doesn’t happen strictly and
immediately after now
ASYNCRONY
31. 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
37. 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
74. 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
76. 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.
78. PROMISES
Promises 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!
79. PROMISES
pending: initial state, not fulfilled or rejected
fulfilled: the operation completed successfully
rejected: meaning that the operation failed.
106. PERFORMANCES
Promises are slower compared to callbacks
You don’t get rid of callbacks, they just orchestrate callbacks
in a trustable way
107. 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
132. 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