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
160
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
85
Loose Coupling with Message Queue / Bus (the short version)
makara
1
100
Asynchronous JS with Promise (+ Workshop)
makara
0
94
Other Decks in Programming
See All in Programming
OCRを使ってゲームのアイテムをデータ化する
kishikawakatsumi
0
120
AI開発を加速するためにテスト戦略を言語化した
yoshihiro_shu
0
100
Composerを使ったサプライチェーン攻撃の様子を眺めてみる #phpstudy
o0h
PRO
2
200
OSもどきOS
arkw
0
360
JJUG CCC 2026 Spring: JSpecify で実現する Kotlin フレンドリーな Java API 設計
ternbusty
1
110
次世代リンターで探る、tsgo 時代における型認識カスタムルールの現実解
ytakahashii
3
1.4k
Moments When Things Go Wrong
aurimas
3
130
Transactional Change Stream Processing With Debezium and Apache Flink
gunnarmorling
1
140
誰も頼んでない機能を出荷した話
zekutax
0
150
AIエージェントの隔離技術の徹底比較
kawayu
0
450
jQueryをバージョンアップする前に使いたいjQuery Migrate
matsuo_atsushi
0
140
さぁV100、メモリをお食べ・・・
nilpe
0
120
Featured
See All Featured
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.8k
AI: The stuff that nobody shows you
jnunemaker
PRO
7
670
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
65
55k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
300
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
570
Unsuck your backbone
ammeep
672
58k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.7k
How Software Deployment tools have changed in the past 20 years
geshan
0
34k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
190
エンジニアに許された特別な時間の終わり
watany
107
240k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Building Flexible Design Systems
yeseniaperezcruz
330
40k
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?