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
72
modernweb 2017
心得分享
CypressKuo
September 20, 2017
Tweet
Share
More Decks by CypressKuo
See All by CypressKuo
Deep-copying.pdf
cypresskuo
0
19
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
17
跟上_JS_的腳步-ES2020.pdf
cypresskuo
0
22
Core_Web_Vitals.pdf
cypresskuo
0
44
這個時代人人都知道的敏捷開發.pdf
cypresskuo
0
42
Webassembly.pdf
cypresskuo
0
8
Other Decks in Technology
See All in Technology
Adopting Jetpack Compose in Your Existing Project - GDG DevFest Bangkok 2024
akexorcist
0
110
Amplify Gen2 Deep Dive / バックエンドの型をいかにしてフロントエンドへ伝えるか #TSKaigi #TSKaigiKansai #AWSAmplifyJP
tacck
PRO
0
390
Why App Signing Matters for Your Android Apps - Android Bangkok Conference 2024
akexorcist
0
130
20241120_JAWS_東京_ランチタイムLT#17_AWS認定全冠の先へ
tsumita
2
300
Making your applications cross-environment - OSCG 2024 NA
salaboy
0
200
Zennのパフォーマンスモニタリングでやっていること
ryosukeigarashi
0
160
Engineer Career Talk
lycorp_recruit_jp
0
190
複雑なState管理からの脱却
sansantech
PRO
1
150
【Pycon mini 東海 2024】Google Colaboratoryで試すVLM
kazuhitotakahashi
2
540
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
0
110
B2B SaaSから見た最近のC#/.NETの進化
sansantech
PRO
0
900
テストコード品質を高めるためにMutation Testingライブラリ・Strykerを実戦導入してみた話
ysknsid25
7
2.7k
Featured
See All Featured
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
Agile that works and the tools we love
rasmusluckow
327
21k
Imperfection Machines: The Place of Print at Facebook
scottboms
265
13k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
93
16k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
10 Git Anti Patterns You Should be Aware of
lemiorhan
655
59k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
Building a Scalable Design System with Sketch
lauravandoore
459
33k
Side Projects
sachag
452
42k
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/