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
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
89
Loose Coupling with Message Queue / Bus (the short version)
makara
1
100
Asynchronous JS with Promise (+ Workshop)
makara
0
96
Other Decks in Programming
See All in Programming
なぜ型を書くのか? TSKaigi2026で改めて考える #tskaigi_smarthr
kajitack
0
120
Even G2とAWSで推しのエージェントを召喚しよう!
har1101
1
120
Creating Composable Callables in Contemporary C++
rollbear
0
160
Honoでのサプライチェーン侵害対策 〜 3つのライブラリに学ぶ
yusukebe
7
1.4k
AI 時代のソフトウェア設計の学び方
masuda220
PRO
29
13k
Javaの型とAI時代に型が大事な理由 / java types and type in AI era
kishida
2
150
Go1.27で導入されるジェネリクスメソッドでできること
mackee
0
160
Make SRE Operations Easier with Azure SRE Agent
kkamegawa
0
7.3k
1B+ /day規模のログを管理する技術
broadleaf
0
100
Spec Driven Development | AI Summit Lisbon
danielsogl
PRO
0
200
AIだと陥りがちなJakarta EE最新技術への移行時の落とし穴と解決策
tnagao7
0
120
フロントエンドとバックエンドで「1文字」を揃えよう
youkidearitai
PRO
0
720
Featured
See All Featured
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
230
How to train your dragon (web standard)
notwaldorf
97
6.7k
How to build a perfect <img>
jonoalderson
1
5.7k
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.9k
A Tale of Four Properties
chriscoyier
163
24k
Speed Design
sergeychernyshev
33
1.9k
Chasing Engaging Ingredients in Design
codingconduct
0
220
Balancing Empowerment & Direction
lara
6
1.2k
Into the Great Unknown - MozCon
thekraken
41
2.6k
A Soul's Torment
seathinner
6
3k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.4k
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
290
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?