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
74
Loose Coupling with Message Queue / Bus (the short version)
makara
1
89
Asynchronous JS with Promise (+ Workshop)
makara
0
77
Other Decks in Programming
See All in Programming
プロジェクト新規参入者のリードタイム短縮の観点から見る、品質の高いコードとアーキテクチャを保つメリット
d_endo
1
1.1k
PLoP 2024: The evolution of the microservice architecture pattern language
cer
PRO
0
2.5k
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
3
1.1k
EventSourcingの理想と現実
wenas
6
2.3k
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
790
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.3k
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
820
開発効率向上のためのリファクタリングの一歩目の選択肢 ~コード分割~ / JJUG CCC 2024 Fall
ryounasso
0
440
シールドクラスをはじめよう / Getting Started with Sealed Classes
mackey0225
3
430
【Kaigi on Rails 2024】YOUTRUST スポンサーLT
krpk1900
1
320
Pinia Colada が実現するスマートな非同期処理
naokihaba
4
220
ふかぼれ!CSSセレクターモジュール / Fukabore! CSS Selectors Module
petamoriken
0
150
Featured
See All Featured
A Modern Web Designer's Workflow
chriscoyier
693
190k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
Raft: Consensus for Rubyists
vanstee
136
6.6k
Imperfection Machines: The Place of Print at Facebook
scottboms
264
13k
Writing Fast Ruby
sferik
627
61k
Building an army of robots
kneath
302
42k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
24k
The Art of Programming - Codeland 2020
erikaheidi
52
13k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.8k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Navigating Team Friction
lara
183
14k
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?