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
82
Other Decks in Programming
See All in Programming
How Android Uses Data Structures Behind The Scenes
l2hyunwoo
0
480
AIを活用し、今後に備えるための技術知識 / Basic Knowledge to Utilize AI
kishida
22
5.8k
ProxyによるWindow間RPC機構の構築
syumai
3
1.2k
AI Coding Agentのセキュリティリスク:PRの自己承認とメルカリの対策
s3h
0
230
今から始めるClaude Code入門〜AIコーディングエージェントの歴史と導入〜
nokomoro3
0
210
Putting The Genie in the Bottle - A Crash Course on running LLMs on Android
iurysza
0
140
Laravel Boost 超入門
fire_arlo
3
220
デザイナーが Androidエンジニアに 挑戦してみた
874wokiite
0
540
私の後悔をAWS DMSで解決した話
hiramax
4
210
FindyにおけるTakumi活用と脆弱性管理のこれから
rvirus0817
0
530
「手軽で便利」に潜む罠。 Popover API を WCAG 2.2の視点で安全に使うには
taitotnk
0
870
AWS発のAIエディタKiroを使ってみた
iriikeita
1
190
Featured
See All Featured
Bash Introduction
62gerente
615
210k
How STYLIGHT went responsive
nonsquared
100
5.8k
Embracing the Ebb and Flow
colly
87
4.8k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
Reflections from 52 weeks, 52 projects
jeffersonlam
352
21k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Thoughts on Productivity
jonyablonski
70
4.8k
Rails Girls Zürich Keynote
gr2m
95
14k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
23
1.4k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
850
The Cult of Friendly URLs
andyhume
79
6.6k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
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?