$30 off During Our Annual Pro Sale. View Details »
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
meet.js Katowice - ES6 Promises 101
Search
Szymon Nowak
March 09, 2016
Programming
1
230
meet.js Katowice - ES6 Promises 101
Szymon Nowak
March 09, 2016
Tweet
Share
More Decks by Szymon Nowak
See All by Szymon Nowak
GDG DevFest 2015 Poland - How to grow your own Babel fish
szimek
0
64
Serving WebP images via content negotiation
szimek
1
84
JSConf EU 2015 - How to grow your own Babel fish
szimek
0
330
Other Decks in Programming
See All in Programming
Rubyで鍛える仕組み化プロヂュース力
muryoimpl
0
150
堅牢なフロントエンドテスト基盤を構築するために行った取り組み
shogo4131
8
2.4k
モデル駆動設計をやってみようワークショップ開催報告(Modeling Forum2025) / model driven design workshop report
haru860
0
270
Navigating Dependency Injection with Metro
l2hyunwoo
1
150
宅宅自以為的浪漫:跟 AI 一起為自己辦的研討會寫一個售票系統
eddie
0
510
UIデザインに役立つ 2025年の最新CSS / The Latest CSS for UI Design 2025
clockmaker
18
7.6k
C-Shared Buildで突破するAI Agent バックテストの壁
po3rin
0
400
これだけで丸わかり!LangChain v1.0 アップデートまとめ
os1ma
6
1.9k
新卒エンジニアのプルリクエスト with AI駆動
fukunaga2025
0
230
ZOZOにおけるAI活用の現在 ~モバイルアプリ開発でのAI活用状況と事例~
zozotech
PRO
9
5.8k
從冷知識到漏洞,你不懂的 Web,駭客懂 - Huli @ WebConf Taiwan 2025
aszx87410
2
2.8k
愛される翻訳の秘訣
kishikawakatsumi
3
330
Featured
See All Featured
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
0
88
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Being A Developer After 40
akosma
91
590k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
0
200
A Modern Web Designer's Workflow
chriscoyier
698
190k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
37
2.7k
Music & Morning Musume
bryan
46
7k
Build your cross-platform service in a week with App Engine
jlugia
234
18k
Done Done
chrislema
186
16k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
400
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
47
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
196
70k
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!