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
Asynchronous JS with Promise
Search
Makara Wang
February 23, 2013
Programming
150
2
Share
Asynchronous JS with Promise
Makara Wang
February 23, 2013
More Decks by Makara Wang
See All by Makara Wang
Code Reuse in Node.js (the short version)
makara
0
82
Loose Coupling with Message Queue / Bus (the short version)
makara
1
97
Asynchronous JS with Promise (+ Workshop)
makara
0
91
Other Decks in Programming
See All in Programming
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
170
ふにゃっとしない名前の付け方 〜哲学で茹で上げる、コシのあるソフトウェア設計〜
shimomura
0
110
20260514_its_the_context_window_stupid.pdf
heita
0
190
なぜあなたのコードには「コシ」がないのか?〜AI時代に問う、最後まで美味しい設計と戦略〜 #phpconkagawa / phpconkagawa2026
shogogg
0
140
セグメントとターゲットを意識するプロポーザルの書き方 〜採択の鍵は、誰に刺すかを見極めるマーケティング戦略にある〜
m3m0r7
PRO
0
750
when storing skills in S3 file
watany
3
1.2k
How We Benchmarked Quarkus: Patterns and anti-patterns
hollycummins
1
180
継続的な負荷検証を目指して
pyama86
0
240
From Formal Specification to Property Based Test
ohbarye
0
710
AIと共に生きる技術選定 2026
sgash708
0
120
How We Practice Exploratory Testing in Iterative Development( #scrumniigata ) / 反復開発の中で、探索的テストをどう実施しているか
teyamagu
PRO
3
690
20年以上続くプロダクトでも使い続けられる静的解析ツールを求めて
matsuo_atsushi
0
140
Featured
See All Featured
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
62
54k
Making the Leap to Tech Lead
cromwellryan
135
9.8k
Speed Design
sergeychernyshev
33
1.6k
Embracing the Ebb and Flow
colly
88
5k
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
190
Making Projects Easy
brettharned
120
6.6k
The World Runs on Bad Software
bkeepers
PRO
72
12k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
180
How STYLIGHT went responsive
nonsquared
100
6.1k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.2k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.5k
Transcript
Asynchronous JS with Promise 王浩宇 Makara Wang
[email protected]
2013.02.23
JS Asynchronous, sometimes not Event driven, sometimes not
Promise http://wiki.commonjs.org/wiki/Promises/A An (eventual) value 3 states: unfulfilled, fulfilled, and
failed
Implementations jQuery Dojo Q - https://github.com/kriskowal/q When.js - https://github.com/cujojs/when Deferred
- https://github.com/medikoo/ deferred
Example with Deferred Load a Backbone.js model Perform some operations
Error handling
function fetch(model) { var def = deferred(); model.fetch({ success: function(model)
{ def.resolve(model); }, error: function() { def.resolve(new Error('lorem')); } }); return def.promise; }
deferred(new Model({id: 1})) .then(fetch) .then(function(model) { // ... }) //
Or .end() .then(function() { // ... }, function(err) { // ... });
Compare with Async Async - https://github.com/caolan/async Think in data Examples:
user login
async.waterfall([ function(callback) { // Load a user model. callback(null, user);
}, function(user, callback) { // Call the login API. user.login({ success: function() { user.authenticated = true; callback(null, user); }, error: function() { callback(new Error('lorem')); } }); } ], function(err, user) {});
// Or simply name it `login` function getLoggedInUser(user) { var
def = deferred(); // Call the login API. user.login({ success: function() { user.authenticated = true; def.resolve(user); }, error: function() { def.resolve(new Error('lorem')); } }); return def.promise; }
Work with Express.js app.get('/', function(req, res, next) { // This
returns a promise. loadSomething(x).end(function(data) { res.send(y); // Or req.y = data; next(); }, next); });
Deferred https://github.com/medikoo/deferred http://www.medikoo.com/asynchronous- javascript
Thanks! Questions?