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
110
UI Synchronicity
eliperelman
1
110
Other Decks in Technology
See All in Technology
新卒1年目、はじめてのアプリケーションサーバー【IBM WebSphere Liberty】
ktgrryt
0
180
SIEMによるセキュリティログの可視化と分析を通じた信頼性向上プロセスと実践
coconala_engineer
1
1.4k
カップ麺の待ち時間(3分)でわかるPartyRockアップデート
ryutakondo
0
160
FODにおけるホーム画面編成のレコメンド
watarukudo
PRO
2
420
メールヘッダーを見てみよう
hinono
0
140
【NGK2025S】動物園(PINTO_model_zoo)に遊びに行こう
kazuhitotakahashi
0
350
クロスアカウントな RDS Snapshot Export による カジュアルなデータ集約の仕組み / 202501-finatext-technight-lt
wa6sn
1
110
【Oracle Cloud ウェビナー】2025年のセキュリティ脅威を読み解く:リスクに備えるためのレジリエンスとデータ保護
oracle4engineer
PRO
1
120
FinJAWS_reinvent2024_recap_database
asahihidehiko
2
240
DMMブックスへのTipKit導入
ttyi2
1
140
商品レコメンドでのexplicit negative feedbackの活用
alpicola
2
500
信頼性を支えるテレメトリーパイプラインの構築 / Building Telemetry Pipeline with OpenTelemetry
ymotongpoo
8
2.8k
Featured
See All Featured
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
174
51k
YesSQL, Process and Tooling at Scale
rocio
170
14k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.3k
VelocityConf: Rendering Performance Case Studies
addyosmani
327
24k
Producing Creativity
orderedlist
PRO
343
39k
Writing Fast Ruby
sferik
628
61k
GraphQLとの向き合い方2022年版
quramy
44
13k
Code Reviewing Like a Champion
maltzj
521
39k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
49
2.2k
Typedesign – Prime Four
hannesfritz
40
2.5k
A better future with KSS
kneath
238
17k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.2k
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!