Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Actually Understanding Asynchronous JavaScript
Search
Steve Kinney
August 04, 2016
Technology
210
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Actually Understanding Asynchronous JavaScript
Steve Kinney
August 04, 2016
More Decks by Steve Kinney
See All by Steve Kinney
Enterprise UI, v2
stevekinney
0
83
React_Performance__2022.pdf
stevekinney
0
34
React Performance v2
stevekinney
0
52
Introduction to Testing
stevekinney
0
170
Web Security, Frontend Masters
stevekinney
0
4k
Making Music with the Web Audio API, JSConf Colombia 2023
stevekinney
0
130
React and TypeScript, Turing School
stevekinney
0
380
Redux Workshop, 2021-05-05
stevekinney
2
2.2k
TypeScript and React Utility Types
stevekinney
1
220
Other Decks in Technology
See All in Technology
ゼロをイチにする仕事が終わったあと
smasato
0
340
Claude Codeとハーネスについて考えてみる
oikon48
18
9.4k
AIレビューはどこまで任せられるのか?自動化と人が背負うレビューの境界
sansantech
PRO
2
770
Devsumi 2026 Summer 人もAIも使える共通基盤を事業の加速装置にする~デザインシステム運用に学ぶ組織レバレッジ~ 渡辺 凌央
legalontechnologies
PRO
0
110
LLMやAIエージェントをソフトウェアに組み込むプラクティス
shibuiwilliam
1
360
最近評価が難しくなった
maroon8021
0
340
CIで使うClaude
iwatatomoya
0
250
なぜ私たちのSREプラクティスはなかなか機能しないのか 〜システムより先に組織を見る〜 / Why our SRE practices aren't really working
vtryo
3
3.7k
完全自律ロボットを作りたくて、先に開発を自律させた話(ROS Japan UG #63 LT)
rryz09
0
510
End-to-Endで考える信頼性 —LINEアプリにおけるクライアント開発×SRE連携の実践
maruloop
4
4.1k
SRE Lounge Hiroshimaへの招待
grimoh
0
640
[2026-07-15] AI Ready なはずだったアーキテクチャと、見えてきた課題・次に目指す状態
wxyzzz
4
2.9k
Featured
See All Featured
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
210
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.4k
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.5k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
52k
Bash Introduction
62gerente
615
220k
Design in an AI World
tapps
1
260
Fireside Chat
paigeccino
42
4k
How Software Deployment tools have changed in the past 20 years
geshan
0
34k
Agile that works and the tools we love
rasmusluckow
331
22k
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
180
WENDY [Excerpt]
tessaabrams
11
38k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
Transcript
Actually Understanding Asynchronous JavaScript
I'm Steve. @stevekinney
I'm Steve. @stevekinney
http://turing.io
http://bit.ly/electronjs
None
PSA: Stop me ay any point to ask any and
all questions.
What does "asynchronous" even mean?
To find out, let's torture some metaphors!
None
Running Errands Synchronously
None
None
None
None
None
None
None
Running Errands Asynchronously
None
None
None
None
None
Functions Run to Completion
None
None
None
None
None
None
None
None
None
None
None
What constitutes "completion?" • Hitting the end of a function
• Reaching a return statement • Throwing an error (and not catching it)
None
What Happens When the Browser Blocks?
What Happens When the Browser Blocks?
None
Asynchronous JavaScript is based on events.
document.addEventListener('click', function () { alert('Congratulations, you clicked on the web
page.'); });
None
The Call Stack A data structure that keeps track of
where we are in the execution of our programs.
var possessions = []; var lifeSavings = 25; function goGroceryShopping()
{ buyVeganHam(); } function buyVeganHam() { if (lifeSavings < 10) { throw new Error('Not enough money.'); } lifeSavings = lifeSavings - 10; possessions.push('vegan ham'); } goGroceryShopping();
Call Stack
Top Level Call Stack
Top Level goGroceryShopping Call Stack
Top Level goGroceryShopping buyVeganHam Call Stack
Top Level goGroceryShopping Call Stack
Top Level Call Stack
Call Stack
var possessions = []; var lifeSavings = 25; function goGroceryShopping()
{ buyOrganicAlmondMilk(); buyVeganHam(); } function buyOrganicAlmondMilk() { lifeSavings = lifeSavings - 20; possessions.push('organic almond milk'); } function buyVeganHam() { // When this code eventually executes, we'll only have $5 left. if (lifeSavings < 10) { throw new Error('Not enough money.'); } lifeSavings = lifeSavings - 10; possessions.push('vegan ham'); } goGroceryShopping();
You've seen the Call Stack before.
Call Stack
Top Level Call Stack
Top Level goGroceryShopping Call Stack
Top Level goGroceryShopping buyOrganicAlmondMilk Call Stack
Top Level goGroceryShopping Call Stack
Top Level goGroceryShopping Call Stack buyVeganHam
Top Level goGroceryShopping Call Stack
Top Level Call Stack
Call Stack
None
The Event Queue A list of functions that are handled
in a first-in, first-out order.
The Event Loop A programming construct that waits for and
dispatches functions in the Event Queue to the Call Stack.
Queue
Queue A very important click event Some mildly important AJAX
response A frivolous click event
Queue Some mildly important AJAX response A frivolous click event
Queue A frivolous click event
Queue
Queue Call Stack
Queue Call Stack Some mildly important AJAX response
Queue Call Stack Some mildly important AJAX response
Queue A very important click event Call Stack
Queue A very important click event A frivolous click event
Call Stack
Queue A very important click event A frivolous click event
Call Stack
Queue A frivolous click event Call Stack
Queue A frivolous click event Call Stack
Queue Call Stack
Turns out that JavaScript isn't asynchronous after all.
It's the environment.
None
None
None
setTimeout(function () { console.log('I will happen later.'); }, 5000); Timers
setTimeout is available in all browsers and Node.js
Timers will be added to the Event Queue after that
number of milliseconds.
Queue Top Level goGroceryShopping buyVeganHams Call Stack
Queue Top Level goGroceryShopping buyVeganHams Call Stack setTimeout(…, 1000)
Queue Top Level goGroceryShopping buyVeganHams Call Stack setTimeout(…, 1000)
Queue Top Level goGroceryShopping buyVeganHams Call Stack thisWillBlockUpTheWorksAndNeverComplete setTimeout(…, 1000)
Queue Top Level goGroceryShopping buyVeganHams Call Stack thisWillBlockUpTheWorksAndNeverComplete setTimeout(…, 1000)
The Callback Pattern
Callbacks aren't anything special.
Functions can be passed as arguments to functions.
function callbackSandwich(callbackFunction) { console.log('Top piece of bread.'); callbackFunction(); console.log('Bottom piece
of bread.'); } callbackSandwich(function () {console.log('American cheese.')});
Sometimes we'll guard against blowing things up if a callback
function isn't provided.
function callbackSandwich(callbackFunction) { console.log('Top piece of bread.'); if (typeof callbackFunction
=== 'function') callbackFunction(); console.log('Bottom piece of bread.'); } callbackSandwich(function () {console.log('American cheese.')}); callbackSandwich(); // Just two pieces of bread.
From Events Listeners to Callbacks
None
None
None
None
None
None
None
None
None
None
None
None
None
So now…
None
None
Promises
Callbacks have some problems.
We can usually only do one thing with the data
from the event.
We can't store the result anywhere.
None
None
None
"Inversion of Control"
None
Libraries: Q, Bluebird, RSVP
None
.then()
Promise Chaining
asyncMultiply(2, 3).then(c => c * 1000).then(c => console.log(c)); Promise Chaining
The return value of the previous link is passed to the next then() method.
None
None
asyncMultiply(2, 3).then(c => c * 1000) .then(c => { throw
new Error('KABOOM!') }) .catch(e => console.error(e));
asyncMultiply(2, 3).then(c => c * 1000) .then(c => { throw
new Error('KABOOM!') })
None
None
None
None
None
None
None
Refactoring Our Callback Pattern
None
None
None
None
None
None
None
Promise.all() http://bit.ly/async- lab-promise-all
Promise.race()
http://turing.io http://bit.ly/electronjs