This document discusses the evolution of asynchronous JavaScript programming. It covers callbacks, promises, generators, and async/await. Callbacks were initially used but led to callback hell. Promises improved the situation by providing a cleaner way to handle asynchronous code but promise hell was still possible. Generators allowed blocking-like code using yield but required promises to handle asynchronous functions. Now async/await in ES2017 allows writing asynchronous code that looks synchronous and resolves many of the past issues. Streams are also mentioned as an alternative concurrency model to callbacks and async code.
12. A JS runtime contains a message queue, which
is a list of messages to be processed.
QUEUE
13. A JS runtime contains a message queue, which
is a list of messages to be processed.
To each message is associated a function.
QUEUE
14. 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
18. 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
19. 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
20.
21. Up until recently (ES2015),
JS itself has actually
never had any direct notion of asynchrony
built into it
22. JS runs inside a hosting environment (the
browser/nodejs)
The event loop is handled by it
23. most devs new to JS have issues with the fact
that later doesn’t happen strictly and
immediately after now
ASYNCRONY
71. Callbacks are the fundamental unit of
asynchrony in JS.
I <3 callbacks
72. 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.
A promise represents a proxy for a value not
necessarily known when the promise is created.
77. 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.
81. PROMISES
pending: initial state, not fulfilled or rejected
fulfilled: the operation completed successfully
rejected: meaning that the operation failed
settled: has fulfilled or rejected
116. PERFORMANCES
Promises are slower compared to callbacks
You don’t get rid of callbacks, they just orchestrate callbacks
in a trustable way
117. 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
143. the iterator should listen for the promise to
resolve (or reject)
then either resume the generator with the
fulfilment message (or throw an error into the
generator with the rejection reason)
GENERATORS + PROMISES