Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Asynchronous JS with Promise (+ Workshop)
Search
Makara Wang
March 30, 2013
Programming
98
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
Jetpack Compose メカニズム
skydoves
0
470
Vue Fes Japan 2026 タイムテーブル徹底解説
448jp
1
510
スマート反転とウェブアクセシビリティ
camiha
0
210
Snowflakeで業務アプリを作ろう。 Snowflakeのアプリ機能解説&実践ガイド
ayumu_yamaguchi
2
300
Heart of Swift Concurrency
koher
0
960
WebRTC映像をAirPlayに対応させる挑戦.pdf
monolithic_adam
0
300
巨大モノリシックアプリ モダン化大作戦
ktcryomm
1
1.1k
仕様駆動開発による爆速プロダクト開発 / Bakusoku Spec Driven Development
kobakei
0
130
フロントエンドUIフレームワークのこれまでとこれから
ssssota
5
2.9k
手動確認はもう限界 〜XCUITestでCustom URL Schemeの遷移を起動種別ごとに自動テストする〜 / Testing Custom URL Schemes with XCUITest
otouto
0
330
AWS DevOps Agentで インシデント対応をAIに任せたい
honmarkhunt
7
3.1k
テストを司るデーモンに会いに行く 〜隔離した仮想マシンでテストを通すまで〜
h1d3mun3
1
570
Featured
See All Featured
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
580
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
260
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
550
Unsuck your backbone
ammeep
672
58k
Optimizing for Happiness
mojombo
378
71k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
320
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
680
ラッコキーワード サービス紹介資料
rakko
1
5M
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Ethics towards AI in product and experience design
skipperchong
2
380
Facilitating Awesome Meetings
lara
57
7.1k
Color Theory Basics | Prateek | Gurzu
gurzu
1
470
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