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 (+ Workshop)
Search
Makara Wang
March 30, 2013
Programming
0
82
Asynchronous JS with Promise (+ Workshop)
Makara Wang
March 30, 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
makara
2
150
Other Decks in Programming
See All in Programming
Migration to Signals, Resource API, and NgRx Signal Store
manfredsteyer
PRO
0
130
バッチ処理を「状態の記録」から「事実の記録」へ
panda728
PRO
0
200
モテるデスク環境
mozumasu
3
1.4k
AI 駆動開発におけるコミュニティと AWS CDK の価値
konokenj
5
290
外接に惑わされない自システムの処理時間SLIをOpenTelemetryで実現した話
kotaro7750
0
120
AIのバカさ加減に怒る前にやっておくこと
blueeventhorizon
0
120
Go言語はstack overflowの夢を見るか?
logica0419
0
650
登壇は dynamic! な営みである / speech is dynamic
da1chi
0
390
Blazing Fast UI Development with Compose Hot Reload (droidcon London 2025)
zsmb
0
400
CSC305 Lecture 10
javiergs
PRO
0
310
三者三様 宣言的UI
kkagurazaka
0
280
Towards Transactional Buffering of CDC Events @ Flink Forward 2025 Barcelona Spain
hpgrahsl
0
120
Featured
See All Featured
A better future with KSS
kneath
239
18k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
jQuery: Nuts, Bolts and Bling
dougneiner
65
7.9k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3k
Optimizing for Happiness
mojombo
379
70k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
51k
Context Engineering - Making Every Token Count
addyosmani
8
320
Intergalactic Javascript Robots from Outer Space
tanoku
272
27k
[RailsConf 2023] Rails as a piece of cake
palkan
57
5.9k
Bash Introduction
62gerente
615
210k
Statistics for Hackers
jakevdp
799
220k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.7k
Transcript
Asynchronous JS with Promise + Workshop 王浩宇 Makara Wang
[email protected]
2013.03.30
Promise A concept 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) { // ... });
Creating function lorem() { var def = deferred(); def.resolve(xxx); return
def.promise; } var promise = lorem();
Creating var promise = deferred(xxx);
Chaining var promise = deferred(xxx); promise.then(function() { return 'a promise
or a value or an error'; }).then(xxx);
var promiseA = deferred(valueA); var promiseB = promiseA.then(function(valueA) { return
valueB; }); var promiseC = promiseB.then(function(valueB) { var def = deferred(); def.resolve(valueC); return def.promise; }); promiseC.then(function(valueC) {});
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(); // A method that calls the login API. user.login({ success: function(data) { var loginUser = new User(data); def.resolve(loginUser); }, 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?
Workshop! https://github.com/devo-ps/practice