Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
meet.js Katowice - ES6 Promises 101
Search
Szymon Nowak
March 09, 2016
Programming
240
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
meet.js Katowice - ES6 Promises 101
Szymon Nowak
March 09, 2016
Transcript
48 slides
SZYMON NOWAK @SZIMEK ES6 PROMISES 101
ISSUES WITH ASYNC CODE
sum(1, 5, (x) => { multiply(x, 3, (y) => {
subtract(y, 5, (z) => { console.log('The result is', z); }); }); });
sum(1, 5) .then((x) => multiply(x, 3)) .then((x) => subtract(x, 5))
.then((x) => { console.log('The result is', x) });
img = document.querySelector('#img'); img.addEventListener('load', onLoaded); img.addEventListener('error', onError);
img = document.querySelector('#img'); img.ready().then( onLoaded, onError );
PROMISES
3 POSSIBLE STATES
PENDING
PENDING RESOLVED
PENDING RESOLVED REJECTED
PENDING -> RESOLVED
PENDING -> REJECTED
STATE CAN CHANGE ONLY ONCE
promise = new Promise(fn);
promise = new Promise((resolve, reject) => { ... });
promise = new Promise((resolve, reject) => { // do something
async... if (success) { resolve(result); } else reject(reason); } });
THEN
promise.then(onResolve)
promise = new Promise((resolve, reject) => { setTimeout(() => resolve(42),
2000); }); function onResolve(result) { console.log(`Resolved with ${result}`); } promise.then(onResolve);
promise = new Promise((resolve, reject) => { setTimeout(() => resolve(42),
2000); }); function onResolve(result) { return result + 10; } promise .then(onResolve) .then(doSomething) .then(doSomethingElse);
sum(1, 5, (x) => { multiply(x, 3, (y) => {
subtract(y, 5, (z) => { console.log('The result is', z); }); }); });
sum(1, 5) .then((x) => multiply(x, 3)) .then((x) => subtract(x, 5))
.then((x) => { console.log('The result is', x) });
promise.then(onResolve)
promise.then(onResolve, onReject)
img = document.querySelector('#img'); img.addEventListener('load', onLoaded); img.addEventListener('error', onError);
img = document.querySelector('#img'); img.ready().then( onLoaded, onError );
CATCH
promise.catch(onReject); // is the same as promise.then(undefined, onReject);
PROMISE.RESOLVE
function authenticate() { if (user) { return Promise.resolve(user); } return
fetchUser(); } authenticate().then((user) => ...)
PROMISE.REJECT
PROMISE.ALL
Promise.all([p1, p2]).then(([r1, r2]) => { // executed when all promises
are resolved });
const urls = [...]; const promises = urls.map(fetch); Promise.all(promises).then((responses) =>
{ // executed when all responses are ready });
PROMISE.RACE
Promise.race([ fetch(url), delay(5000).then(() => throw new Error('Timed out’) ) ]);
ERROR HANDLING
new Promise((resolve, reject) => { throw new Error(‘bazinga!’); }); //
exception inside promises // are translated to rejections
new Promise((resolve, reject) => { try { throw new Error(‘bazinga!’);
} catch(error) { reject(error); } });
const promise = fetch(url); promise .then(step1) .then(step2) .then(step3) .catch(onError) .then(step4)
fetch(url) .then(onSuccess, onError); // is not the same as fetch(url)
.then(onSuccess) .catch(onError);
ISSUES
CANNOT BE CANCELLED
CANNOT BE USED WITH STREAMS OF DATA
NATIVE APIS
FETCH WEBRTC SERVICE WORKER …
THANK YOU!
More Decks by Szymon Nowak
See All by Szymon Nowak
GDG DevFest 2015 Poland - How to grow your own Babel fish
szimek
0
76
Serving WebP images via content negotiation
szimek
1
110
JSConf EU 2015 - How to grow your own Babel fish
szimek
0
340
Other Decks in Programming
See All in Programming
LLM 101
dalinaum
0
540
クラウドエージェントは、 何がそんなに便利なのか / what makes cloud agents so useful
rkaga
5
980
Ruby から Emacs を操作したい
phigasui
0
390
小さなアプリを作って公開するまで
onozaty
0
240
imagewebp()の向こう側: WebPはなぜ JPEG・PNGより軽いのか
yuzneri
0
150
AppFunctionsの内部実装を紐解く
hiroaki404
0
120
JavaScriptはシングルバイナリの夢を見るか / Does JavaScript Dream of a Single Binary?
ssssota
0
150
AWS CDKを理解するために公式の定義を深掘って解説してみる / Understanding AWS CDK by Digging into Its Official Definition
akihisaikeda
3
340
AI Augmentation Techniques with Liberty
ivargrimstad
1
560
例外からRich Errorsまで Kotlinエラーハンドリングの進化を読み解く
shiita0903
0
240
Spec-Driven Development - Why Vaadin Is the Perfect Fit
simas
PRO
0
260
AI時代のシニアエンジニアとは
yosuke_furukawa
PRO
15
12k
Featured
See All Featured
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
620
KATA
mclloyd
PRO
35
16k
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
620
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
2
690
SEO for Brand Visibility & Recognition
aleyda
0
4.9k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.9k
A Tale of Four Properties
chriscoyier
163
24k
Thoughts on Productivity
jonyablonski
76
5.5k
Ten Tips & Tricks for a 🌱 transition
stuffmc
1
290
Designing Experiences People Love
moore
143
24k
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
4
1.3k
The Language of Interfaces
destraynor
162
27k