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
81
Other Decks in Programming
See All in Programming
GoのWebAssembly活用パターン紹介
syumai
3
10k
Julia という言語について (FP in Julia « SIDE: F ») for 関数型まつり2025
antimon2
3
920
Datadog RUM 本番導入までの道
shinter61
1
260
AIコーディング道場勉強会#2 君(エンジニア)たちはどう生きるか
misakiotb
1
160
実はすごいスピードで進化しているCSS
hayato_yokoyama
0
110
生成AIで日々のエラー調査を進めたい
yuyaabo
0
530
FormFlow - Build Stunning Multistep Forms
yceruto
1
160
Webからモバイルへ Vue.js × Capacitor 活用事例
naokihaba
0
550
関数型まつり2025登壇資料「関数プログラミングと再帰」
taisontsukada
2
800
Enterprise Web App. Development (2): Version Control Tool Training Ver. 5.1
knakagawa
1
110
ワンバイナリWebサービスのススメ
mackee
10
7.7k
事業戦略を理解してソフトウェアを設計する
masuda220
PRO
22
6k
Featured
See All Featured
Become a Pro
speakerdeck
PRO
28
5.4k
How to Ace a Technical Interview
jacobian
276
23k
We Have a Design System, Now What?
morganepeng
52
7.6k
GitHub's CSS Performance
jonrohan
1031
460k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
32
5.9k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
47
2.8k
Raft: Consensus for Rubyists
vanstee
139
7k
For a Future-Friendly Web
brad_frost
179
9.8k
Code Review Best Practice
trishagee
68
18k
The Pragmatic Product Professional
lauravandoore
35
6.7k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
228
22k
Music & Morning Musume
bryan
46
6.6k
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?