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
meet.js Katowice - ES6 Promises 101
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Szymon Nowak
March 09, 2016
Programming
230
1
Share
meet.js Katowice - ES6 Promises 101
Szymon Nowak
March 09, 2016
More Decks by Szymon Nowak
See All by Szymon Nowak
GDG DevFest 2015 Poland - How to grow your own Babel fish
szimek
0
65
Serving WebP images via content negotiation
szimek
1
91
JSConf EU 2015 - How to grow your own Babel fish
szimek
0
330
Other Decks in Programming
See All in Programming
Going Multiplatform with Your Android App (Android Makers 2026)
zsmb
2
390
Radical Imagining - LIFT 2025-2027 Policy Agenda
lift1998
0
270
実践ハーネスエンジニアリング #MOSHTech
kajitack
7
6.3k
Laravel Nightwatchの裏側 - Laravel公式Observabilityツールを支える設計と実装
avosalmon
1
330
RSAが破られる前に知っておきたい 耐量子計算機暗号(PQC)入門 / Intro to PQC: Preparing for the Post-RSA Era
mackey0225
3
130
ルールルルルルRubyの中身の予備知識 ── RubyKaigiの前に予習しなイカ?
ydah
1
150
Symfonyの特性(設計思想)を手軽に活かす特性(trait)
ickx
0
130
PHP 7.4でもOpenTelemetryゼロコード計装がしたい! / PHPerKaigi 2026
arthur1
1
570
Running Swift without an OS
kishikawakatsumi
0
780
Coding at the Speed of Thought: The New Era of Symfony Docker
dunglas
0
4.8k
Feature Toggle は捨てやすく使おう
gennei
0
570
「接続」—パフォーマンスチューニングの最後の一手 〜点と点を結ぶ、その一瞬のために〜
kentaroutakeda
5
2.5k
Featured
See All Featured
Designing Powerful Visuals for Engaging Learning
tmiket
1
340
Principles of Awesome APIs and How to Build Them.
keavy
128
17k
Navigating Weather and Climate Data
rabernat
0
160
How to Think Like a Performance Engineer
csswizardry
28
2.5k
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Building an army of robots
kneath
306
46k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1k
The Invisible Side of Design
smashingmag
302
51k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
480
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
1.9k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
93
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.2k
Transcript
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!