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
2
150
Asynchronous JS with Promise
Makara Wang
February 23, 2013
Tweet
Share
More Decks by Makara Wang
See All by Makara Wang
Code Reuse in Node.js (the short version)
makara
0
80
Loose Coupling with Message Queue / Bus (the short version)
makara
1
92
Asynchronous JS with Promise (+ Workshop)
makara
0
82
Other Decks in Programming
See All in Programming
速いWebフレームワークを作る
yusukebe
3
1.5k
Google I/O recap web編 大分Web祭り2025
kponda
0
2.9k
tool ディレクティブを導入してみた感想
sgash708
1
160
コンテキストエンジニアリング Cursor編
kinopeee
1
740
RDoc meets YARD
okuramasafumi
4
160
Updates on MLS on Ruby (and maybe more)
sylph01
1
170
Protocol Buffersの型を超えて拡張性を得る / Beyond Protocol Buffers Types Achieving Extensibility
linyows
0
100
Honoアップデート 2025年夏
yusukebe
1
900
「手軽で便利」に潜む罠。 Popover API を WCAG 2.2の視点で安全に使うには
taitotnk
0
220
MCPで実現するAIエージェント駆動のNext.jsアプリデバッグ手法
nyatinte
7
1k
rage against annotate_predecessor
junk0612
0
150
オープンセミナー2025@広島「君はどこで動かすか?」アンケート結果
satoshi256kbyte
0
230
Featured
See All Featured
Imperfection Machines: The Place of Print at Facebook
scottboms
268
13k
Making the Leap to Tech Lead
cromwellryan
134
9.5k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
284
13k
4 Signs Your Business is Dying
shpigford
184
22k
Facilitating Awesome Meetings
lara
55
6.5k
Embracing the Ebb and Flow
colly
87
4.8k
Why Our Code Smells
bkeepers
PRO
339
57k
How STYLIGHT went responsive
nonsquared
100
5.8k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.5k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.8k
Being A Developer After 40
akosma
90
590k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
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?