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
80
Loose Coupling with Message Queue / Bus (the short version)
makara
1
92
Asynchronous JS with Promise (+ Workshop)
makara
0
81
Other Decks in Programming
See All in Programming
A2A プロトコルを試してみる
azukiazusa1
2
1.2k
Azure AI Foundryではじめてのマルチエージェントワークフロー
seosoft
0
130
「ElixirでIoT!!」のこれまでとこれから
takasehideki
0
370
Beyond Portability: Live Migration for Evolving WebAssembly Workloads
chikuwait
0
390
ReadMoreTextView
fornewid
1
480
VS Code Update for GitHub Copilot
74th
1
400
関数型まつり2025登壇資料「関数プログラミングと再帰」
taisontsukada
2
850
XSLTで作るBrainfuck処理系
makki_d
0
210
Cursor AI Agentと伴走する アプリケーションの高速リプレイス
daisuketakeda
1
130
ニーリーにおけるプロダクトエンジニア
nealle
0
500
Google Agent Development Kit でLINE Botを作ってみた
ymd65536
2
190
20250628_非エンジニアがバイブコーディングしてみた
ponponmikankan
0
440
Featured
See All Featured
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
48
2.8k
The World Runs on Bad Software
bkeepers
PRO
69
11k
Mobile First: as difficult as doing things right
swwweet
223
9.7k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Measuring & Analyzing Core Web Vitals
bluesmoon
7
490
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
2.9k
Into the Great Unknown - MozCon
thekraken
39
1.9k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
Rebuilding a faster, lazier Slack
samanthasiow
82
9.1k
Writing Fast Ruby
sferik
628
61k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
50k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
8
670
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?