Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
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
93
Asynchronous JS with Promise (+ Workshop)
makara
0
84
Other Decks in Programming
See All in Programming
俺流レスポンシブコーディング 2025
tak_dcxi
14
9.2k
Kotlin Multiplatform Meetup - Compose Multiplatform 외부 의존성 아키텍처 설계부터 운영까지
wisemuji
0
110
tsgolintはいかにしてtypescript-goの非公開APIを呼び出しているのか
syumai
7
2.3k
Giselleで作るAI QAアシスタント 〜 Pull Requestレビューに継続的QAを
codenote
0
250
re:Invent 2025 のイケてるサービスを紹介する
maroon1st
0
140
ZOZOにおけるAI活用の現在 ~モバイルアプリ開発でのAI活用状況と事例~
zozotech
PRO
9
5.8k
ローカルLLMを⽤いてコード補完を⾏う VSCode拡張機能を作ってみた
nearme_tech
PRO
0
120
Claude Codeの「Compacting Conversation」を体感50%減! CLAUDE.md + 8 Skills で挑むコンテキスト管理術
kmurahama
1
580
Rubyで鍛える仕組み化プロヂュース力
muryoimpl
0
150
愛される翻訳の秘訣
kishikawakatsumi
3
330
GISエンジニアから見たLINKSデータ
nokonoko1203
0
170
Navigation 3: 적응형 UI를 위한 앱 탐색
fornewid
1
420
Featured
See All Featured
The browser strikes back
jonoalderson
0
65
Embracing the Ebb and Flow
colly
88
4.9k
Building the Perfect Custom Keyboard
takai
1
660
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
110
Joys of Absence: A Defence of Solitary Play
codingconduct
1
250
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
0
89
Navigating Weather and Climate Data
rabernat
0
44
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
60
37k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.3k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
180
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
120
GraphQLとの向き合い方2022年版
quramy
50
14k
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?