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
150
2
Share
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
82
Loose Coupling with Message Queue / Bus (the short version)
makara
1
97
Asynchronous JS with Promise (+ Workshop)
makara
0
91
Other Decks in Programming
See All in Programming
PicoRuby for IoT: Connecting to the Cloud with MQTT
yuuu
2
750
アクセシビリティ試験の"その後"を仕組み化する
yuuumiravy
1
190
Spec-Driven Development with AI Agents (Workshop, May 2026)
antonarhipov
2
300
How We Practice Exploratory Testing in Iterative Development( #scrumniigata ) / 反復開発の中で、探索的テストをどう実施しているか
teyamagu
PRO
3
690
cloudnative conference 2026 flyle
azihsoyn
0
100
Back to the roots of date
jinroq
0
700
ハーネスエンジニアリングとは?
kinopeee
13
6.8k
ローカルLLMでどこまでコードが書けるか / How much code can be written on a local LLM
kishida
2
310
〜バイブコーディングを超えて〜 チームで実験し続けたAI駆動開発
tigertora7571
0
190
ハーネスエンジニアリングにどう向き合うか 〜ルールファイルを超えて開発プロセスを設計する〜 / How to approach harness engineering
rkaga
28
19k
tRPCの概要と少しだけパフォーマンス
misoton665
2
260
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
130
Featured
See All Featured
The Curse of the Amulet
leimatthew05
1
12k
Crafting Experiences
bethany
1
140
Measuring & Analyzing Core Web Vitals
bluesmoon
9
820
Building Adaptive Systems
keathley
44
3k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.4k
Ethics towards AI in product and experience design
skipperchong
2
270
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
120
Deep Space Network (abreviated)
tonyrice
0
130
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
180
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.2k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.7k
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?