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
380
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
130
UI Synchronicity
eliperelman
1
150
Other Decks in Technology
See All in Technology
What happened to RubyGems and what can we learn?
mikemcquaid
0
180
ファインディの横断SREがTakumi byGMOと取り組む、セキュリティと開発スピードの両立
rvirus0817
1
800
いよいよ仕事を奪われそうな波が来たぜ
kazzpapa3
3
320
Introduction to Bill One Development Engineer
sansan33
PRO
0
350
Sansan Engineering Unit 紹介資料
sansan33
PRO
1
3.8k
しろおびセキュリティへ ようこそ
log0417
0
230
AIとともに歩む情報セキュリティ / Information Security with AI
kanny
4
3k
名刺メーカーDevグループ 紹介資料
sansan33
PRO
0
1k
15 years with Rails and DDD (AI Edition)
andrzejkrzywda
0
130
ゼロから始めたFindy初のモバイルアプリ開発
grandbig
2
590
今日から始めるAmazon Bedrock AgentCore
har1101
4
300
JuliaTokaiとしてはこれが最後かもしれない(仮) for NGK2026S
antimon2
0
130
Featured
See All Featured
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
9.5k
How to make the Groovebox
asonas
2
1.9k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Color Theory Basics | Prateek | Gurzu
gurzu
0
190
Unsuck your backbone
ammeep
671
58k
Building the Perfect Custom Keyboard
takai
2
680
The Language of Interfaces
destraynor
162
26k
Abbi's Birthday
coloredviolet
1
4.6k
From π to Pie charts
rasagy
0
120
Crafting Experiences
bethany
1
44
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.3k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
86
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!