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
modernweb 2017
Search
CypressKuo
September 20, 2017
Technology
0
73
modernweb 2017
心得分享
CypressKuo
September 20, 2017
Tweet
Share
More Decks by CypressKuo
See All by CypressKuo
Deep-copying.pdf
cypresskuo
0
20
CSS_for_Web_Vitals.pdf
cypresskuo
0
28
談談_Code_review.pdf
cypresskuo
0
36
REST_vs_GraphQL_今夜はご注文はどっち.pdf
cypresskuo
0
15
CSS_重構.pdf
cypresskuo
0
18
跟上_JS_的腳步-ES2020.pdf
cypresskuo
0
22
Core_Web_Vitals.pdf
cypresskuo
0
47
這個時代人人都知道的敏捷開發.pdf
cypresskuo
0
43
Webassembly.pdf
cypresskuo
0
8
Other Decks in Technology
See All in Technology
dbtを中心にして組織のアジリティとガバナンスのトレードオンを考えてみた
gappy50
0
310
Amazon Route 53, 待ちに待った TLSAレコードのサポート開始
kenichinakamura
0
180
データ基盤におけるIaCの重要性とその運用
mtpooh
4
550
DMMブックスへのTipKit導入
ttyi2
1
110
2025年に挑戦したいこと
molmolken
0
170
今年一年で頑張ること / What I will do my best this year
pauli
1
220
完全自律型AIエージェントとAgentic Workflow〜ワークフロー構築という現実解
pharma_x_tech
0
360
2024年活動報告会(人材育成推進WG・ビジネスサブWG) / 20250114-OIDF-J-EduWG-BizSWG
oidfj
0
240
Unsafe.BitCast のすゝめ。
nenonaninu
0
200
KMP with Crashlytics
sansantech
PRO
0
250
comilioとCloudflare、そして未来へと向けて
oliver_diary
6
460
AWS re:Invent 2024 re:Cap Taipei (for Developer): New Launches that facilitate Developer Workflow and Continuous Innovation
dwchiang
0
170
Featured
See All Featured
Statistics for Hackers
jakevdp
797
220k
How STYLIGHT went responsive
nonsquared
96
5.3k
Building a Scalable Design System with Sketch
lauravandoore
460
33k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
127
18k
Become a Pro
speakerdeck
PRO
26
5.1k
Measuring & Analyzing Core Web Vitals
bluesmoon
5
210
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
3
360
Making the Leap to Tech Lead
cromwellryan
133
9k
Speed Design
sergeychernyshev
25
740
Gamification - CAS2011
davidbonilla
80
5.1k
Product Roadmaps are Hard
iamctodd
PRO
50
11k
Keith and Marios Guide to Fast Websites
keithpitt
410
22k
Transcript
ModernWeb 2017 ⼼心得報告分享 2017/09/20 前端組 CypressKuo
關於 JavaScript 非同步的那些事兒 By 陳鋒逸(陳⼩小風)
Agenda • JavaScript 與非同步 • Callback & Promise • Iterator
& Generator • Async & Await
JavaScript 與非同步
None
JavaScript • 單執⾏行行緒 • 非同步 • 非阻塞式
單執⾏行行緒 • 核⼼心特徵 • 瀏覽器導向 • 避免複雜性 • HTML5 Web
Worker
非同步/非阻塞 打電話訂書 店員:稍等,我查⼀一下 店員回覆 等待 店員:我查⼀一下,晚點再 回覆你 店員回電 等待 做其他事
乾等 同步 非同步 (非阻塞) (阻塞)
None
Call Stack Web Apis Callback Queue http://latentflip.com/loupe
Call Stack Web Apis Callback Queue http://latentflip.com/loupe i = 0
Call Stack Web Apis Callback Queue http://latentflip.com/loupe i < 4
Call Stack Web Apis Callback Queue http://latentflip.com/loupe setTimeout(function () {
console.log(i); }, 0)
Call Stack Web Apis Callback Queue http://latentflip.com/loupe Anonymous() i ++
Call Stack Web Apis Callback Queue http://latentflip.com/loupe Anonymous() Anonymous() i
< 4
Call Stack Web Apis Callback Queue http://latentflip.com/loupe setTimeout(function () {
console.log(i); }, 0) Anonymous() Anonymous()
Call Stack Web Apis Callback Queue http://latentflip.com/loupe Anonymous() i ++
Anonymous() Anonymous()
Call Stack Web Apis Callback Queue http://latentflip.com/loupe Anonymous() Anonymous() i
< 4 Anonymous() Anonymous()
…
Call Stack Web Apis Callback Queue http://latentflip.com/loupe Anonymous() Anonymous() Anonymous()
Anonymous() Anonymous() Anonymous() Anonymous() Anonymous()
Call Stack Web Apis Callback Queue http://latentflip.com/loupe Anonymous() Anonymous() Anonymous()
Anonymous() Anonymous() Anonymous() Anonymous() Anonymous()
Call Stack Web Apis Callback Queue http://latentflip.com/loupe Anonymous() Anonymous() Anonymous()
Anonymous() Anonymous() Anonymous() Anonymous() Anonymous() Console.log(i)
Call Stack Web Apis Callback Queue http://latentflip.com/loupe Anonymous() Anonymous() Anonymous()
Anonymous() Anonymous() Anonymous() Anonymous() Anonymous()
Call Stack Web Apis Callback Queue http://latentflip.com/loupe Anonymous() Anonymous() Anonymous()
Anonymous() Anonymous() Anonymous() Anonymous()
Call Stack Web Apis Callback Queue http://latentflip.com/loupe Anonymous() Anonymous() Anonymous()
Anonymous() Anonymous() Anonymous() Anonymous()
Call Stack Web Apis Callback Queue http://latentflip.com/loupe Anonymous() Anonymous() Anonymous()
Anonymous() Anonymous() Anonymous() Anonymous() Console.log(i)
PIXNET wiki Front-end Team cctsao - JavaScript VM 如何解析 Code
cctsao - JavaScript Async & Event Loop
Callback & Promise
Callback • 非同步 • 滿⾜足情境時觸發 • 複雜邏輯不好維護 • 較難處理理錯誤 圖片出處:https://brunolm.wordpress.com/2017/01/13/using-asyncawait-in-typescript-javascript/
Promise • 兼容性 • 狀狀態明確 • 可以連續執⾏行行 • 更更好維護/閱讀
Promise Success Failure
Can I Use Promise? https://caniuse.com/#search=promise
Iterator & Generator
Iterator • 佔記憶體空間 • 記憶體讀寫花時間 • 處理理 10000 個物件
Generator • ⼀一次⼀一個 • 停在上次執⾏行行之處 • 節省記憶體
Generator + co library • 暫時交出執⾏行行權 • 封裝非同步任務,處理理 Promise •
邏輯清楚好理理解 • 讓 Generator ⾃自動執⾏行行 https://github.com/tj/co
Async & Await
Async & Await • 結合 Promise 與 Generator + co
• 不需要第三⽅方套件 • 將非同步化程式同步化 • 直覺性的邏輯思考 • ES7, TypeScript, Node 7.6 ⽀支援
Async & Await • async - 標著在 function 前⾯面 •
await - 標著在 promise 前⾯面 • ⼀一定要⽤用 function 包裝
Can I Use Async? https://caniuse.com/#search=promise
回顧
Callback https://caniuse.com/#search=promise
Promise https://caniuse.com/#search=promise
Generator + co https://caniuse.com/#search=promise
async & await https://caniuse.com/#search=promise
總結 • 善⽤用非同步 • 學習新寫法、新技術 • 理理解運作機制 • 節省開發/除錯時間
學習新技術,讓我們更更優雅更更快速解決問題
Reference • http://s.itho.me/modernweb/2017/day2/403-3-陳⼩小風.pdf • https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/ Promise • https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Statements/ async_function •
https://medium.com/@peterchang_82818/javascript-es7-async-await-教 學-703473854f29-tutorial-example-703473854f29 • https://jigsawye.com/2016/04/18/understanding-javascript-async-await/ • https://mlwmlw.org/2016/03/es7-的-async-await-入⾨門/ • https://noootown.wordpress.com/2016/11/13/callback-promise-fetch-yield-async- await/