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
76
Loose Coupling with Message Queue / Bus (the short version)
makara
1
91
Asynchronous JS with Promise (+ Workshop)
makara
0
79
Other Decks in Programming
See All in Programming
Spring gRPC について / About Spring gRPC
mackey0225
0
200
Compose でデザインと実装の差異を減らすための取り組み
oidy
1
280
Open source software: how to live long and go far
gaelvaroquaux
0
480
AHC041解説
terryu16
0
560
チームリードになって変わったこと
isaka1022
0
160
Simple組み合わせ村から大都会Railsにやってきた俺は / Coming to Rails from the Simple
moznion
3
4k
月刊 競技プログラミングをお仕事に役立てるには
terryu16
2
1.3k
ファインディの テックブログ爆誕までの軌跡
starfish719
2
910
ESLintプラグインを使用してCDKのセオリーを適用する
yamanashi_ren01
2
470
動作確認やテストで漏れがちな観点3選
starfish719
6
930
社内フレームワークとその依存性解決 / in-house framework and its dependency management
vvakame
1
520
CloudNativePGがCNCF Sandboxプロジェクトになったぞ! 〜CloudNativePGの仕組みの紹介〜
nnaka2992
0
210
Featured
See All Featured
How GitHub (no longer) Works
holman
313
140k
Adopting Sorbet at Scale
ufuk
74
9.2k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
193
16k
GraphQLとの向き合い方2022年版
quramy
44
13k
Reflections from 52 weeks, 52 projects
jeffersonlam
348
20k
How to train your dragon (web standard)
notwaldorf
90
5.8k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
7k
4 Signs Your Business is Dying
shpigford
182
22k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
YesSQL, Process and Tooling at Scale
rocio
171
14k
Fashionably flexible responsive web design (full day workshop)
malarkey
406
66k
Optimising Largest Contentful Paint
csswizardry
33
3.1k
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?