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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
81
Loose Coupling with Message Queue / Bus (the short version)
makara
1
94
Asynchronous JS with Promise (+ Workshop)
makara
0
84
Other Decks in Programming
See All in Programming
MDN Web Docs に日本語翻訳でコントリビュート
ohmori_yusuke
0
650
SourceGeneratorのススメ
htkym
0
200
Rust 製のコードエディタ “Zed” を使ってみた
nearme_tech
PRO
0
170
フロントエンド開発の勘所 -複数事業を経験して見えた判断軸の違い-
heimusu
7
2.8k
React 19でつくる「気持ちいいUI」- 楽観的UIのすすめ
himorishige
11
7.4k
2026年 エンジニアリング自己学習法
yumechi
0
130
Patterns of Patterns
denyspoltorak
0
1.4k
AI Schema Enrichment for your Oracle AI Database
thatjeffsmith
0
280
Oxlintはいいぞ
yug1224
5
1.3k
Oxlint JS plugins
kazupon
1
930
登壇資料を作る時に意識していること #登壇資料_findy
konifar
4
1.1k
CSC307 Lecture 06
javiergs
PRO
0
680
Featured
See All Featured
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
77
4 Signs Your Business is Dying
shpigford
187
22k
Paper Plane
katiecoart
PRO
0
46k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
61k
Balancing Empowerment & Direction
lara
5
890
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
96
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
170
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
130
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
0
270
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
5.2k
Ruling the World: When Life Gets Gamed
codingconduct
0
140
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?