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
86
0
Share
Asynchronous JS with Promise (+ Workshop)
Makara Wang
March 30, 2013
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
94
Asynchronous JS with Promise
makara
2
150
Other Decks in Programming
See All in Programming
KagglerがMixSeekを触ってみた
morim
0
370
「速くなった気がする」をデータで疑う
senleaf24
0
150
それはエンジニアリングの糧である:AI開発のためにAIのOSSを開発する現場より / It serves as fuel for engineering: insights from the field of developing open-source AI for AI development.
nrslib
1
830
Codex CLIのSubagentsによる並列API実装 / Parallel API Implementation with Codex CLI Subagents
takatty
2
870
Strategy for Finding a Problem for OSS: With Real Examples
kibitan
0
140
SkillがSkillを生む:QA観点出しを自動化した
sontixyou
6
3.2k
見せてもらおうか、 OpenSearchの性能とやらを!
shunta27
1
180
Going Multiplatform with Your Android App (Android Makers 2026)
zsmb
2
360
ふりがな Deep Dive try! Swift Tokyo 2026
watura
0
170
ネイティブアプリとWebフロントエンドのAPI通信ラッパーにおける共通化の勘所
suguruooki
0
250
LM Linkで(非力な!)ノートPCでローカルLLM
seosoft
0
420
PHPで TLSのプロトコルを実装してみるをもう一度しゃべりたい
higaki_program
0
180
Featured
See All Featured
Side Projects
sachag
455
43k
How STYLIGHT went responsive
nonsquared
100
6k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.4k
Practical Orchestrator
shlominoach
191
11k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4k
Leo the Paperboy
mayatellez
7
1.6k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.6k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.3k
Rails Girls Zürich Keynote
gr2m
96
14k
Making Projects Easy
brettharned
120
6.6k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
160
Java REST API Framework Comparison - PWX 2021
mraible
34
9.3k
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