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
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
AI Agent Dojo #4: watsonx Orchestrate ADK体験
oniak3ibm
PRO
0
130
rack-attack gemによるリクエスト制限の失敗と学び
pndcat
0
230
TerraformとStrands AgentsでAmazon Bedrock AgentCoreのSSO認証付きエージェントを量産しよう!
neruneruo
4
2.5k
20251212 AI 時代的 Legacy Code 營救術 2025 WebConf
mouson
0
250
AtCoder Conference 2025「LLM時代のAHC」
imjk
2
680
疑似コードによるプロンプト記述、どのくらい正確に実行される?
kokuyouwind
0
290
Architectural Extensions
denyspoltorak
0
190
Fragmented Architectures
denyspoltorak
0
120
実は歴史的なアップデートだと思う AWS Interconnect - multicloud
maroon1st
0
340
コマンドとリード間の連携に対する脅威分析フレームワーク
pandayumi
1
400
なぜSQLはAIぽく見えるのか/why does SQL look AI like
florets1
0
330
Fluid Templating in TYPO3 14
s2b
0
100
Featured
See All Featured
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
0
1.8k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
290
A Modern Web Designer's Workflow
chriscoyier
698
190k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
85
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
0
520
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
89
The Pragmatic Product Professional
lauravandoore
37
7.1k
Build your cross-platform service in a week with App Engine
jlugia
234
18k
SEO for Brand Visibility & Recognition
aleyda
0
4.2k
Evolving SEO for Evolving Search Engines
ryanjones
0
100
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.3k
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
0
1.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?