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
You don't know Promises - IT Konekt April 2019
Search
Ivan Jovanovic
April 20, 2019
Technology
1
47
You don't know Promises - IT Konekt April 2019
Ivan Jovanovic
April 20, 2019
Tweet
Share
More Decks by Ivan Jovanovic
See All by Ivan Jovanovic
Micro Frontends - A microservice approach to the modern web - Oredev 2019
ivanjov
0
70
Micro Frontends - A microservice approach to the modern web - O'Reilly Software Architecture Berlin 2019
ivanjov
0
58
React State - The Good, the Bad and the Ugly - ArmadaJS
ivanjov
0
90
Thinking Reactive in JavaScript - Sphere.it JS Krakow
ivanjov
0
49
Micro Frontends - A microservice approach to the modern web - PHPSrbija
ivanjov
0
200
Micro Frontends - A microservice approach to the modern web - Codemotion Rome 2019
ivanjov
0
320
Mikro Frontendi - Uvod - Novi Sad JS
ivanjov
0
130
Software engineer marketing done right – career tips from my experience.
ivanjov
0
29
Micro Frontends - A microservice approach to the modern web - Full Stack London 2018
ivanjov
0
180
Other Decks in Technology
See All in Technology
Work as an App Engineer
lycorp_recruit_jp
0
330
クレカ・銀行連携機能における “状態”との向き合い方 / SmartBank Engineer LT Event
smartbank
2
100
終了の危機にあった15年続くWebサービスを全力で存続させる - phpcon2024
yositosi
27
22k
PHP ユーザのための OpenTelemetry 入門 / phpcon2024-opentelemetry
shin1x1
3
1.4k
NW-JAWS #14 re:Invent 2024(予選落ち含)で 発表された推しアップデートについて
nagisa53
0
270
Opcodeを読んでいたら何故かphp-srcを読んでいた話
murashotaro
0
310
ゼロから創る横断SREチーム 挑戦と進化の軌跡
rvirus0817
2
270
なぜCodeceptJSを選んだか
goataka
0
170
10分で学ぶKubernetesコンテナセキュリティ/10min-k8s-container-sec
mochizuki875
3
380
LINEヤフーのフロントエンド組織・体制の紹介【24年12月】
lycorp_recruit_jp
0
540
Wantedly での Datadog 活用事例
bgpat
1
590
React Routerで実現する型安全なSPAルーティング
sansantech
PRO
2
210
Featured
See All Featured
Unsuck your backbone
ammeep
669
57k
Done Done
chrislema
182
16k
Designing for humans not robots
tammielis
250
25k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Practical Orchestrator
shlominoach
186
10k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
26
1.9k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
520
Rails Girls Zürich Keynote
gr2m
94
13k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
45
2.2k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
Transcript
You don't know Promises
I am Ivan Jovanovic Senior Software Engineer, Tech Speaker You
can find me at @ivanjov96 or https://ivanjov.com Hello! IJ Consulting
Why I don't know Promises?
None
None
I am here today To show you how Promises really
work!
JavaScript is strange because ‒ It was built for 10
days ‒ It has one thread ‒ It is synchronous and asynchronous ‒ It is everywhere ‒ It is different
Only one thread And the event loop
None
How do we handle async code? Promises Callbacks Async/await
Callbacks Where everything began... 1
“ A callback is a function that is passed as
an argument and will be executed after another function has finished executing.
None
None
Promises Changed everything 2
“ The Promise object represents the eventual completion (or failure)
of an asynchronous operation, and its resulting value.
None
None
How Promises really work? The answer is a Microtask queue!
None
Microtask queue ‒ Independent queue ‒ Used by: process.nextTick, Promises,
Object.observe, MutationObserver ‒ Faster than the main queue and may be drained several times in a single event loop tick
None
Next TICK ONE Next TICK TWO TIMEOUT AFTER-ONE TIMEOUT AFTER-TWO
Promise methods ‒ Promise.all(iterable) ‒ Promise.race(iterable) ‒ Promise.resolve(value) ‒ Promise.reject(reason)
Promise.all(iterable)
Rejected situation
Promise.race(iterable)
Promisify non-Promise code
Native Promises from libraries
Async/await Comes to the rescue 3
“ Async/Await is a special syntax for working with Promises.
None
Most common problems with Promises that we see every day!
9
Nested promises 1
None
None
Broken Promise chain 2
None
None
Mixing sync and async code 3
None
None
None
Missing catch 4
None
None
None
Forget to return a Promise 5
None
None
Promisified synchronous code 6
None
Mixing Promise and Async/Await 7
None
None
Async function that returns Promise 8
None
None
None
Define a callback as an async function 9
None
Clinic.js https://clinicjs.org/
Conclusion ‒ JS is crazy and great at the same
time! ‒ Async behavior is a pro, not a con. ‒ Be careful when using Promises, it's great and dangerous at the same time!
Any questions ? You can find me at ‒ @ivanjov96
‒
[email protected]
Thanks!