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
320
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
100
UI Synchronicity
eliperelman
1
110
Other Decks in Technology
See All in Technology
podman_update_2024-12
orimanabu
1
260
DevOps視点でAWS re:invent2024の新サービス・アプデを振り返ってみた
oshanqq
0
180
【re:Invent 2024 アプデ】 Prompt Routing の紹介
champ
0
140
LINE Developersプロダクト(LIFF/LINE Login)におけるフロントエンド開発
lycorptech_jp
PRO
0
120
5分でわかるDuckDB
chanyou0311
10
3.2k
[Ruby] Develop a Morse Code Learning Gem & Beep from Strings
oguressive
1
150
統計データで2024年の クラウド・インフラ動向を眺める
ysknsid25
2
840
多領域インシデントマネジメントへの挑戦:ハードウェアとソフトウェアの融合が生む課題/Challenge to multidisciplinary incident management: Issues created by the fusion of hardware and software
bitkey
PRO
2
100
re:Invent 2024 Innovation Talks(NET201)で語られた大切なこと
shotashiratori
0
300
Fanstaの1年を大解剖! 一人SREはどこまでできるのか!?
syossan27
2
160
GitHub Copilot のテクニック集/GitHub Copilot Techniques
rayuron
26
11k
KubeCon NA 2024 Recap / Running WebAssembly (Wasm) Workloads Side-by-Side with Container Workloads
z63d
1
240
Featured
See All Featured
Testing 201, or: Great Expectations
jmmastey
40
7.1k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.6k
Into the Great Unknown - MozCon
thekraken
33
1.5k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.4k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
Side Projects
sachag
452
42k
How to Ace a Technical Interview
jacobian
276
23k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.9k
Become a Pro
speakerdeck
PRO
26
5k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
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!