Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Asynchronous JS with Promise
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Makara Wang
February 23, 2013
Programming
160
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
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
91
Loose Coupling with Message Queue / Bus (the short version)
makara
1
110
Asynchronous JS with Promise (+ Workshop)
makara
0
98
Other Decks in Programming
See All in Programming
Apple Intelligence を用いた個人情報誤送信防止、及びユーザーリクエスト体験の改善について
yukiny
0
350
現場と育てるRailsモデリング 〜Spreadsheetから始めたモデル設計〜
siroemk
2
510
UPDATE をやめる — EF Core でマスタをバージョン管理する
panda728
PRO
0
1.2k
IBM Bob Dojo #1 仕様駆動開発入門
oniak3ibm
PRO
0
390
【加筆修正版】Laravel のアプリケーションをどこにデプロイするか #phpcon_ehime
akase244
0
150
GemmaをJevのように使ってみる / Use Gemma like Jev
kishida
5
780
カツオ、ご期待ください
suneo3476
0
180
C#の現在地 進化の歴史と、AI時代の.NET Everywhere
neuecc
5
5.1k
Simple Storage Service(S3) is not simple
iwatsukayura
0
280
SalesForceを内製化!? ~ HR事業を支える顧客管理基盤のインフラを大公開 ~
oku053
0
170
PumaからFalconに移行したRailsアプリケーションでの実践
falcon8823
0
150
wkhtmltopdfの次どうするか問題2026
willnet
2
1.9k
Featured
See All Featured
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
50
10k
For a Future-Friendly Web
brad_frost
184
10k
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
780
Joys of Absence: A Defence of Solitary Play
codingconduct
1
550
Thoughts on Productivity
jonyablonski
76
5.4k
GitHub's CSS Performance
jonrohan
1033
470k
SEO for Brand Visibility & Recognition
aleyda
0
4.8k
Why You Should Never Use an ORM
jnunemaker
PRO
61
10k
Mind Mapping
helmedeiros
1
400
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.4k
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
410
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
850
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?