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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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
1k
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
レビュー履歴をAIに食わせて、 Compose移行を加速するs
shihochan
0
240
Oxlintはいいぞ(続)
yug1224
1
490
属人化した知識を、 AIが辿れる地図にする
pkshadeck
PRO
1
220
Deep dive into the select statement (GopherCon UK)
jespino
0
160
わからない話を追いかけたら、プログラミング言語を作る側にいた
ydah
3
570
片田舎のおっさん、 Swift Buildのダイアモンド問題解決の不具合修正PRを出すが、解決方法がキャッシュをしないようにすることであり、ビルド時間が伸びると言われてマージされないので高速化もする/swiftbuild
yimajo
0
320
typoなんかねぇよ
raspython3
0
620
TSX の <Hoge<Fuga>> という構文に驚いた話 / tsx-type-argument-syntax
kanaru0928
0
240
in-process GraphQL のすすめ #ginzajs
izumin5210
4
1.5k
30年振りにコンパイラの定数整数除算を改善した
herumi
9
4.3k
源内ハンズオン概要編
hideg
0
230
Gmail/Google DriveをトリガーにAIエージェントを動かそう! / Run AI agents with Gmail/Google Drive as triggers!
har1101
3
460
Featured
See All Featured
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
290
Art, The Web, and Tiny UX
lynnandtonic
304
22k
sira's awesome portfolio website redesign presentation
elsirapls
0
350
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
2
3.8k
The World Runs on Bad Software
bkeepers
PRO
72
12k
The untapped power of vector embeddings
frankvandijk
2
1.8k
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
270
First, design no harm
axbom
PRO
2
1.3k
How to build a perfect <img>
jonoalderson
1
5.9k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
480
Thoughts on Productivity
jonyablonski
76
5.3k
4 Signs Your Business is Dying
shpigford
187
23k
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