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
100
JSConf EU 2015 - How to grow your own Babel fish
szimek
0
340
Other Decks in Programming
See All in Programming
仕様駆動開発入門 (何を作るかを先に決めて渡す) | 目指せ Kiro マスター 05
inariku
PRO
2
160
AIで開発が速くなった先で、開発チームは何を目指すか
shihochan
1
2.1k
graphql-ruby を Spinel で速くしようとした話
mtsmfm
0
150
Modernizing iOS Apps
seyfoyun
0
150
AI時代のシニアエンジニアとは
yosuke_furukawa
PRO
15
12k
Lambda durable functionsでHuman in the Loopを楽に…楽に……
har1101
2
320
地方こそサーバーレス — 10年続けた、顔が見えるチームで確かな基盤を / Serverless Matters Most in Regional Areas — 10 Years of Building Reliable Foundations with Close-Knit Teams
seike460
PRO
1
450
自作Discord音声botをLambda MicroVMsに移行した話 / Migrating My Discord Voice Bot to Lambda MicroVMs
akihisaikeda
1
320
はじめてのPLATEAU
nokonoko1203
1
650
PyConTW 2026 - AI Coding 不只是工具升級, 區域醫院醫療資訊系統協作開發與資訊團隊轉型實踐
cclai999
0
250
作って学ぶSpring BootのAuto Configuration / JJUG Night Seminar 2026-10-19
rshindo
0
230
AIが実装を担う時代の「技術設計」と「チームマネジメント」
kamina_zzz
2
260
Featured
See All Featured
The Invisible Side of Design
smashingmag
301
52k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
580
The Spectacular Lies of Maps
axbom
PRO
1
1.1k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
2k
The untapped power of vector embeddings
frankvandijk
2
2k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
480
Unsuck your backbone
ammeep
672
58k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.9k
The World Runs on Bad Software
bkeepers
PRO
72
12k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
1
660
Typedesign – Prime Four
hannesfritz
42
3.2k
Mind Mapping
helmedeiros
1
440