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
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
61
Serving WebP images via content negotiation
szimek
1
76
JSConf EU 2015 - How to grow your own Babel fish
szimek
0
330
Other Decks in Programming
See All in Programming
都市をデータで見るってこういうこと PLATEAU属性情報入門
nokonoko1203
1
570
型付きアクターモデルがもたらす分散シミュレーションの未来
piyo7
0
810
Team topologies and the microservice architecture: a synergistic relationship
cer
PRO
0
1.1k
ruby.wasmで多人数リアルタイム通信ゲームを作ろう
lnit
2
280
データの民主化を支える、透明性のあるデータ利活用への挑戦 2025-06-25 Database Engineering Meetup#7
y_ken
0
320
「ElixirでIoT!!」のこれまでとこれから
takasehideki
0
370
Azure AI Foundryではじめてのマルチエージェントワークフロー
seosoft
0
140
関数型まつりレポート for JuliaTokai #22
antimon2
0
160
Java on Azure で LangGraph!
kohei3110
0
170
PHPで始める振る舞い駆動開発(Behaviour-Driven Development)
ohmori_yusuke
2
230
Code as Context 〜 1にコードで 2にリンタ 34がなくて 5にルール? 〜
yodakeisuke
0
110
システム成長を止めない!本番無停止テーブル移行の全貌
sakawe_ee
1
140
Featured
See All Featured
Designing Experiences People Love
moore
142
24k
How to Ace a Technical Interview
jacobian
277
23k
Unsuck your backbone
ammeep
671
58k
Designing for humans not robots
tammielis
253
25k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
48
2.8k
Agile that works and the tools we love
rasmusluckow
329
21k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
5
220
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
252
21k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
229
22k
The World Runs on Bad Software
bkeepers
PRO
69
11k
Raft: Consensus for Rubyists
vanstee
140
7k
Documentation Writing (for coders)
carmenintech
72
4.9k
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!