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
JavaScript Promises
Search
Lean Machine
September 04, 2013
Programming
1.1k
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
JavaScript Promises
Lean Machine
September 04, 2013
More Decks by Lean Machine
See All by Lean Machine
Graceful Degradation with Modernizr
leanmachine
1
1k
Intro to HTML5
leanmachine
3
990
Organizing Stylesheets with CSS Pre-processors
leanmachine
2
960
Responsive Web Design in a Nutshell
leanmachine
3
140
Seven UX Design Rules
leanmachine
9
1k
JavaScript Closures
leanmachine
1
1.1k
JavaScript Inheritance
leanmachine
2
1.1k
Asynchronous JavaScript
leanmachine
1
970
Other Decks in Programming
See All in Programming
エンジニアと一緒にテストコードの設計と実装を改善した話
mototakatsu
0
170
ECSアプリログをFireLensでコスト削減しようとしたけど諦めた話 in Fargate×Node.js
akihisaikeda
2
4.2k
dRuby over BLE
makicamel
2
330
JavaDoc 再入門
nagise
0
330
[2026年度第1回ORセミナー] 計画最適化ベンチャーと競技プログラミング人材
terryu16
0
260
RTSPクライアントを自作してみた話
simotin13
0
600
ローカルLLMでどこまでコードが書けるか -拡張版 / How much code can be written on a local LLM Extended
kishida
10
4k
Dataformのリポジトリを立ち上げるときにまずやること / dataform-day0-2026
snhryt
0
160
例外の正しい扱い方 そのエラー try-catchして大丈夫?
jinwatanabe
0
230
Technical Debt: Understanding it Rightly, Engaging it Rightly #LaravelLiveJP
shogogg
0
220
Composerを使ったサプライチェーン攻撃の様子を眺めてみる #phpstudy
o0h
PRO
2
250
ふつうのFeature Flag実践入門
irof
7
3.9k
Featured
See All Featured
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.1k
WCS-LA-2024
lcolladotor
0
630
Skip the Path - Find Your Career Trail
mkilby
1
150
4 Signs Your Business is Dying
shpigford
187
22k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
270
How to Talk to Developers About Accessibility
jct
2
230
Exploring anti-patterns in Rails
aemeredith
3
410
Leo the Paperboy
mayatellez
7
1.8k
SEO for Brand Visibility & Recognition
aleyda
0
4.6k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.4k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
62
44k
Transcript
var api = require('./obfuscated/api'); var async = require('async'); async.parallel([
function(callback) { api('http//api.somesite.com/getCurrentUser', function(err, currentUser) { if (err) { callback(err); } else { api('http//api.somesite.com/user/' + currentUser.id + '/friends', callback); } }); }, function(callback) { api('http//api.somesite.com/listPeople', callback); } ], function(err, res) { var currentUserFriends = res[0]; var listPeople = res[1]; var notFriends = listPeople.length -‐ currentUserFriends.length; console.log("not friended: " + notFriends); });
var util = require('./obfuscated/util'); var currentUser = util.api('http//api.somesite.com/getCurrentUser'); var urlToCurrentUserFriends
= util.concatStrings('http//api.somesite.com/user/', currentUser.get('id'), '/friends'); var currentFriends = util.api(urlToCurrentUserFriends); var allPeople = util.api('http//api.somesite.com/listPeople'); var notFriends = util.subtract(allPeople.get('length'), currentFriends.get('length')); util.print(util.concatStrings('not friended: ', notFriends));
Promises No more callbacks leanmachine.se ▪
[email protected]
▪ 2013-09-06
A promise is an object that represents the return value
or the thrown exception that a function may eventually provide.
var api = require('./obfuscated/api'); var async = require('async'); async.parallel([
function(callback) { api('http//api.somesite.com/getCurrentUser', function(err, currentUser) { if (err) { callback(err); } else { api('http//api.somesite.com/user/' + currentUser.id + '/friends', callback); } }); }, function(callback) { api('http//api.somesite.com/listPeople', callback); } ], function(err, res) { var currentUserFriends = res[0]; var listPeople = res[1]; var notFriends = listPeople.length -‐ currentUserFriends.length; console.log("not friended: " + notFriends); });
var util = require('./obfuscated/util'); var currentUser = util.api('http//api.somesite.com/getCurrentUser'); var urlToCurrentUserFriends
= util.concatStrings('http//api.somesite.com/user/', currentUser.get('id'), '/friends'); var currentFriends = util.api(urlToCurrentUserFriends); var allPeople = util.api('http//api.somesite.com/listPeople'); var notFriends = util.subtract(allPeople.get('length'), currentFriends.get('length')); util.print(util.concatStrings('not friended: ', notFriends));
var util = require('./obfuscated/util'); var currentUser = util.api('http//api.somesite.com/getCurrentUser'); var urlToCurrentUserFriends
= util.concatStrings('http//api.somesite.com/user/', currentUser.get('id'), '/friends'); var currentFriends = util.api(urlToCurrentUserFriends); var allPeople = util.api('http//api.somesite.com/listPeople'); var notFriends = util.subtract(allPeople.get('length'), currentFriends.get('length')); util.print(util.concatStrings('not friended: ', notFriends)); currentUserFriends.then(function(value) { console.log("current user friends:") console.log(value); });
Coding time!
leanmachine.se ▪
[email protected]
▪ 2013-09-06