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 (+ Workshop)
Search
Makara Wang
March 30, 2013
Programming
99
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
92
Loose Coupling with Message Queue / Bus (the short version)
makara
1
110
Asynchronous JS with Promise
makara
2
160
Other Decks in Programming
See All in Programming
[DroidKnights 2026] Workflow AX: From Auto-Documented Policies to E2E Testing
raeminkang
0
550
AI駆動開発4年目の現在地(『あなたはFDEになるべきか?』イベント資料)
kumaiu
0
570
社内AIエージェント「Askワンバン」における コストチューニングの取り組み
t0yohei
0
170
React Native経験者が Flutterを30⽇間触って驚いたこと
3saito
0
170
Engineering Biometric UX Across Android Versions | DroidKnights 2026
ryunen344
1
190
Web Framework Benchmark Battle 2026, Any%
a_matsuda
3
3.2k
Python Lambdaにおけるテストしやすい設計とAI支援の活用 / Designing Testable Python Lambda Functions with AI
kitano_yuichi
1
310
動画プレイヤーはなぜカクつくのか 〜Flutterのレンダリングとネイティブの境界を解剖する〜
naruogram
0
330
Enforcing Your Architecture with Unit Tests in the AI Era (Swift Connection X)
steliosf
PRO
1
160
AIが実装を担う時代の「技術設計」と「チームマネジメント」
kamina_zzz
2
250
Modernizing iOS Apps
seyfoyun
0
150
Kawaiicon: Kubernetes, it's no longer cool, but let's secure it anyway!
barnbarn
0
130
Featured
See All Featured
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
630
Darren the Foodie - Storyboard
khoart
PRO
4
4.1k
The Art of Programming - Codeland 2020
erikaheidi
57
14k
We Have a Design System, Now What?
morganepeng
55
8.4k
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.7k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
480
Mind Mapping
helmedeiros
1
440
Game over? The fight for quality and originality in the time of robots
wayneb77
1
330
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
340
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8.2k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
123
22k
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
2
2.2k
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