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
0
79
Asynchronous JS with Promise (+ Workshop)
Makara Wang
March 30, 2013
Tweet
Share
More Decks by Makara Wang
See All by Makara Wang
Code Reuse in Node.js (the short version)
makara
0
76
Loose Coupling with Message Queue / Bus (the short version)
makara
1
91
Asynchronous JS with Promise
makara
2
150
Other Decks in Programming
See All in Programming
ASP. NET CoreにおけるWebAPIの最新情報
tomokusaba
0
310
知られざるDMMデータエンジニアの生態 〜かつてツチノコと呼ばれし者〜
takaha4k
3
1.1k
カンファレンス動画鑑賞会のススメ / Osaka.swift #1
hironytic
0
210
DROBEの生成AI活用事例 with AWS
ippey
0
110
個人アプリを2年ぶりにアプデしたから褒めて / I just updated my personal app, praise me!
lovee
0
320
Alba: Why, How and What's So Interesting
okuramasafumi
0
240
Immutable ActiveRecord
megane42
0
130
WebDriver BiDiとは何なのか
yotahada3
1
120
自分ひとりから始められる生産性向上の取り組み #でぃーぷらすオオサカ
irof
8
2.2k
Terraform で作る Amazon ECS の CI/CD パイプライン
hiyanger
0
120
Pythonでもちょっとリッチな見た目のアプリを設計してみる
ueponx
1
320
CloudNativePGがCNCF Sandboxプロジェクトになったぞ! 〜CloudNativePGの仕組みの紹介〜
nnaka2992
0
210
Featured
See All Featured
The Power of CSS Pseudo Elements
geoffreycrofte
75
5.4k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
49
2.2k
Bash Introduction
62gerente
610
210k
For a Future-Friendly Web
brad_frost
176
9.5k
Why Our Code Smells
bkeepers
PRO
335
57k
GraphQLの誤解/rethinking-graphql
sonatard
68
10k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Side Projects
sachag
452
42k
The Language of Interfaces
destraynor
156
24k
It's Worth the Effort
3n
184
28k
Java REST API Framework Comparison - PWX 2021
mraible
28
8.4k
Into the Great Unknown - MozCon
thekraken
34
1.6k
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