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
97
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
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
91
Loose Coupling with Message Queue / Bus (the short version)
makara
1
110
Asynchronous JS with Promise
makara
2
160
Other Decks in Programming
See All in Programming
Claude CodeとAgentCore Gatewayを繋ぐ際の認証認可 / Authentication and authorization when connecting Claude Code with AgentCore Gateway
har1101
2
420
AI駆動開発にグラフDBを重ねてみた
satoshi256kbyte
2
640
業務時間外もAIに働いてもらう話
colorful12
3
9.8k
DynamoDBの基礎を振り返りながらベクトル検索機能を理解する
musan
3
260
typoなんかねぇよ
raspython3
0
650
이 함수, 실패하면 어떻게 되나요? null부터 Rich Errors까지, Kotlin 에러 처리 15년
haeti2
0
110
関東Kaggler会_NVIDIA_Nemotron_コンペ_振り返り
rick_ds
0
800
AIの中の人になってみる
htkym
0
150
PHPプロジェクトの結合バランスを可視化する #php_night
kajitack
0
190
MySQLとPostgreSQLって何が違うの?
akagami
PRO
0
160
Can LLMs Replicate 4 Years of Compose Migration? Exploring the boundaries of automation with 279 XML files from a real product
makun
0
120
高専キャリア LT 発表内容
crysta1221
6
5.4k
Featured
See All Featured
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
260
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
640
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
260
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
Heart Work Chapter 1 - Part 1
lfama
PRO
8
36k
It's Worth the Effort
3n
188
29k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
490
The agentic SEO stack - context over prompts
schlessera
0
880
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
220
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.5k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
480
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