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
Zoom2Youtube.Claude
kawaguti
PRO
3
490
凡エンジニアがこの先生きのこるためには。〜TypeScript完全に理解したい〜
alchemy1115
2
180
「早く出す」より「事業に効く」 ── 顧客の業務サイクルから逆算するAI時代の二重ループ開発と「変化の設計者」 / devsumi2026
rakus_dev
1
230
CIで使うClaude
iwatatomoya
0
250
プロダクトだけじゃない、社内プロセスにおける自動化・省力化ノススメ
kakehashi
PRO
1
3.7k
Amazon EVS で VCF 9.0 / 9.1 のサポート開始まとめ
mtoyoda
0
290
LLMやAIエージェントをソフトウェアに組み込むプラクティス
shibuiwilliam
1
360
地域 SRE コミュニティ最前線 / SRE NEXT 2026 Discussion Night Track C
muziyoshiz
0
210
AI Agent SaaS を支える自社仮想化基盤への挑戦と実運用 / ai-agent-saas-virtualization
flatt_security
2
3.8k
AIレビューはどこまで任せられるのか?自動化と人が背負うレビューの境界
sansantech
PRO
2
770
「ちゃんとやっている」は独りよがりだった ― 不安に寄り添うインシデント対応へ / Towards incident response that addresses anxieties
chmikata
1
5.1k
AIと共生する開発者プラットフォーム:バクラクのモノレポ×マイクロサービス基盤
sakajunquality
2
3.5k
Featured
See All Featured
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
410
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
420
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
390
Context Engineering - Making Every Token Count
addyosmani
9
1k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
650
The SEO identity crisis: Don't let AI make you average
varn
0
510
Believing is Seeing
oripsolob
1
170
Music & Morning Musume
bryan
47
7.3k
How Software Deployment tools have changed in the past 20 years
geshan
0
34k
Crafting Experiences
bethany
1
210
It's Worth the Effort
3n
188
29k
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