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
jQuery Deferreds and Promises
Search
Eli Perelman
November 01, 2011
Technology
10
310
jQuery Deferreds and Promises
My introduction slides from the Omaha Front-end Web and jQuery meetup on November 1, 2011.
Eli Perelman
November 01, 2011
Tweet
Share
More Decks by Eli Perelman
See All by Eli Perelman
Intro to Raptor
eliperelman
0
97
UI Synchronicity
eliperelman
1
110
Other Decks in Technology
See All in Technology
Exadata Database Service on Dedicated Infrastructure(ExaDB-D) UI スクリーン・キャプチャ集
oracle4engineer
PRO
2
3.2k
dev 補講: プロダクトセキュリティ / Product security overview
wa6sn
1
2.3k
OCI 運用監視サービス 概要
oracle4engineer
PRO
0
4.8k
なぜ今 AI Agent なのか _近藤憲児
kenjikondobai
4
1.3k
ISUCONに強くなるかもしれない日々の過ごしかた/Findy ISUCON 2024-11-14
fujiwara3
8
870
DMARC 対応の話 - MIXI CTO オフィスアワー #04
bbqallstars
1
160
OCI Network Firewall 概要
oracle4engineer
PRO
0
4.1k
ExaDB-D dbaascli で出来ること
oracle4engineer
PRO
0
3.8k
Evangelismo técnico: ¿qué, cómo y por qué?
trishagee
0
360
ノーコードデータ分析ツールで体験する時系列データ分析超入門
negi111111
0
410
Application Development WG Intro at AppDeveloperCon
salaboy
0
180
【Startup CTO of the Year 2024 / Audience Award】アセンド取締役CTO 丹羽健
niwatakeru
0
930
Featured
See All Featured
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Ruby is Unlike a Banana
tanoku
97
11k
Facilitating Awesome Meetings
lara
50
6.1k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
370
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
47
2.1k
Designing on Purpose - Digital PM Summit 2013
jponch
115
7k
Producing Creativity
orderedlist
PRO
341
39k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
Transcript
jQuery Deferreds & Promises Eli Perelman @eliperelman
What are deferreds?
Provide a system to register callbacks into neatly managed queues
DEFERREDS
WITHOUT DEFERREDS var validateUser = function ( username, password, callback
) { if ( username === ‘bob’ && password === ‘123’ ) { callback( ‘success’ ); } else { callback( ‘failure’ ); } }; validateUser( ‘bob’, ‘123’, function ( result ) { if ( result === ‘success’ ) { location = ‘/dashboard’; } else { showError( ‘Invalid login.’ ); } });
WITH DEFERREDS var validateUser = function ( username, password )
{ var deferred = $.Deferred(); if ( username === ‘bob’ && password === ‘123’ ) { deferred.resolve(); } else { deferred.reject(); } return deferred.promise(); }; validateUser( ‘bob’, ‘123’ ) .done( function () { location = ‘/dashboard’; }) .fail( function () { showError( ‘Invalid login.’ ); });
Deferreds work with synchronous AND asynchronous functions.
Since jQuery 1.5, the $.ajax module uses deferreds.
What are promises?
Provide a common API for consuming code to register callbacks,
sync or async. PROMISES
Deferreds Promises .then .then .done .done .fail .fail .always .always
.reject .resolve .pipe .pipe .promise .promise .isResolved .isRejected
Coding time!