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
970
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
AWS CDK を「作」ってみた 〜フルスクラッチで見えた CDK の裏側〜 / aws-cdk-from-scratch
gotok365
3
2.8k
【やさしく解説 設計編・中級 #4】ルールの寿命と、システムの年輪
panda728
PRO
2
180
仕様書を書く前にハーネスを作る - Agent Native開発は「探索を速く、判定を固く」
gotalab555
4
1.6k
【やさしく解説 設計編・中級 #1】一つの車に、運転手は一人 ~ある倉庫システムの事例から~
panda728
PRO
0
210
JAWS-UG横浜 #102 AWSサ終供養LT会 成仏できない AWS サービスたち 〜本日、三体供養します〜
maroon1st
0
330
自動化したのに回らないテスト運用の壁ーAI時代の品質責任と生産性
mfunaki
0
130
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
540
OpenSpecのproposalにbrainstormingを持たせてみた
tigertora7571
1
210
Apache Hive: そしてCloud Native Lakehouseへ
okumin
1
210
5分で問診!Composer セキュリティ健康診断
codmoninc
0
900
ここ半年くらいでAIに作らせたR用ツール
eitsupi
0
370
【QA Test Talk Vol.8】AI-DLC による Whole Team Approach の加速
pkshadeck
PRO
0
120
Featured
See All Featured
30 Presentation Tips
portentint
PRO
1
360
Large-scale JavaScript Application Architecture
addyosmani
515
110k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
400
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
10k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
66
57k
Navigating Team Friction
lara
192
16k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2k
Marketing to machines
jonoalderson
1
5.7k
The Invisible Side of Design
smashingmag
301
52k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.1k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
620
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
480
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