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 (+ Workshop)
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Makara Wang
March 30, 2013
Programming
96
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Asynchronous JS with Promise (+ Workshop)
Makara Wang
March 30, 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
makara
2
160
Other Decks in Programming
See All in Programming
さぁV100、メモリをお食べ・・・
nilpe
0
150
ローカルLLMを使ってB2Bサービスを作っていての学び
yaotti
0
200
Spec Driven Development | AI Summit Lisbon
danielsogl
PRO
0
200
jQueryをバージョンアップする前に使いたいjQuery Migrate
matsuo_atsushi
0
570
AI 輔助遺留系統現代化的經驗分享
jame2408
1
910
フロントエンドとバックエンドで「1文字」を揃えよう
youkidearitai
PRO
0
720
トークンをケチるな、設計しろ:GitHub Copilotを賢く使うコンテキスト戦略
ochtum
0
120
「AIで開発し、AIを届ける」をEvalでつなぐ 〜AIネイティブに始めるプロダクト開発の実践〜 / Connecting "Develop with AI, deliver AI" with Eval
rkaga
4
5.3k
Agentic UI
manfredsteyer
PRO
0
180
Performance Engineering for Everyone
elenatanasoiu
0
200
JavaDoc 再入門
nagise
1
370
A2UI という光を覗いてみる
satohjohn
1
140
Featured
See All Featured
Into the Great Unknown - MozCon
thekraken
41
2.6k
Crafting Experiences
bethany
1
180
Navigating Weather and Climate Data
rabernat
0
230
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
160
sira's awesome portfolio website redesign presentation
elsirapls
0
280
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
390
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.3k
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
140
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Designing for Performance
lara
611
70k
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
2k
Embracing the Ebb and Flow
colly
88
5.1k
Transcript
Asynchronous JS with Promise + Workshop 王浩宇 Makara Wang
[email protected]
2013.03.30
Promise A concept 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) { // ... });
Creating function lorem() { var def = deferred(); def.resolve(xxx); return
def.promise; } var promise = lorem();
Creating var promise = deferred(xxx);
Chaining var promise = deferred(xxx); promise.then(function() { return 'a promise
or a value or an error'; }).then(xxx);
var promiseA = deferred(valueA); var promiseB = promiseA.then(function(valueA) { return
valueB; }); var promiseC = promiseB.then(function(valueB) { var def = deferred(); def.resolve(valueC); return def.promise; }); promiseC.then(function(valueC) {});
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(); // A method that calls the login API. user.login({ success: function(data) { var loginUser = new User(data); def.resolve(loginUser); }, 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?
Workshop! https://github.com/devo-ps/practice