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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
38
React_Performance__2022.pdf
stevekinney
0
31
React Performance v2
stevekinney
0
48
Introduction to Testing
stevekinney
0
170
Web Security, Frontend Masters
stevekinney
0
3.9k
Making Music with the Web Audio API, JSConf Colombia 2023
stevekinney
0
120
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
2026TECHFRESH畢業分享會 - 葬送的通靈師:化系統與用戶雜訊成行動訊號
line_developers_tw
PRO
0
1.3k
Kubernetesにおける学習基盤とLLMOpsの概要
ry
1
320
Android の公式 Skill / Android skills
yanzm
0
160
【NRUG vol.18】KubernetesにおけるNew Relicデータ取得量削減の考え方
nrug_member
0
170
アンオフィシャルな、オフィシャルからのお願い
wyamazak_devrel
0
140
20260619 私の日常業務での生成 AI 活用
masaruogura
1
230
データサイエンスを価値につなげるプロジェクト設計 〜 DS一年目が現場で得た気づき 〜
ysd113
1
280
Chainlitで作るお手軽チャットUI
ynt0485
0
280
Kiro Ambassador を目指す話
k_adachi_01
0
110
2026TECHFRESH畢業分享會 - AI 時代的人生存檔點
line_developers_tw
PRO
0
1.3k
2026年6月23日 Syncable Tech + Start Python Club にて
hamukazu
0
140
Flow 不死:AI 時代 DevOps 的不變本質
cheng_wei_chen
2
340
Featured
See All Featured
Balancing Empowerment & Direction
lara
6
1.2k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
Reality Check: Gamification 10 Years Later
codingconduct
0
2.2k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
340
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.5k
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
1
350
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
New Earth Scene 8
popppiees
3
2.3k
sira's awesome portfolio website redesign presentation
elsirapls
0
280
Faster Mobile Websites
deanohume
310
31k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
850
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