Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
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
370
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
120
UI Synchronicity
eliperelman
1
140
Other Decks in Technology
See All in Technology
SQLだけでマイグレーションしたい!
makki_d
0
1.1k
Fashion×AI「似合う」を届けるためのWEARのAI戦略
zozotech
PRO
2
1k
AI駆動開発における設計思想 認知負荷を下げるフロントエンドアーキテクチャ/ 20251211 Teppei Hanai
shift_evolve
PRO
2
460
re:Invent2025 3つの Frontier Agents を紹介 / introducing-3-frontier-agents
tomoki10
0
320
日本Rubyの会: これまでとこれから
snoozer05
PRO
5
210
ActiveJobUpdates
igaiga
1
270
GitHub Copilotを使いこなす 実例に学ぶAIコーディング活用術
74th
3
3.6k
Database イノベーショントークを振り返る/reinvent-2025-database-innovation-talk-recap
emiki
0
240
たまに起きる外部サービスの障害に備えたり備えなかったりする話
egmc
0
340
フルカイテン株式会社 エンジニア向け採用資料
fullkaiten
0
9.8k
mairuでつくるクレデンシャルレス開発環境 / Credential-less development environment using Mailru
mirakui
5
570
100以上の新規コネクタ提供を可能にしたアーキテクチャ
ooyukioo
0
150
Featured
See All Featured
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.6k
Believing is Seeing
oripsolob
0
12
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
34k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.2k
Optimising Largest Contentful Paint
csswizardry
37
3.5k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.1k
Building Applications with DynamoDB
mza
96
6.8k
Building the Perfect Custom Keyboard
takai
1
660
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.1k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
9.1k
Everyday Curiosity
cassininazir
0
110
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
0
75
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!