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
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
130
Other Decks in Technology
See All in Technology
[VPoE Global Summit] サービスレベル目標による信頼性への投資最適化
satos
0
190
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
12
81k
初めてのDatabricks Apps開発
taka_aki
1
220
Introdução a Service Mesh usando o Istio
aeciopires
1
250
CoRL 2025 Survey
harukiabe
1
230
React19.2のuseEffectEventを追う
maguroalternative
2
560
今この時代に技術とどう向き合うべきか
gree_tech
PRO
2
2.1k
Oracle Autonomous AI Database:サービス概要のご紹介
oracle4engineer
PRO
2
15k
CREが作る自己解決サイクルSlackワークフローに組み込んだAIによる社内ヘルプデスク改革 #cre_meetup
bengo4com
0
110
Railsの話をしよう
yahonda
0
170
Azureコストと向き合った、4年半のリアル / Four and a half years of dealing with Azure costs
aeonpeople
1
230
Copilot Studio ハンズオン - 生成オーケストレーションモード
tomoyasasakimskk
0
160
Featured
See All Featured
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
990
The Pragmatic Product Professional
lauravandoore
36
7k
Product Roadmaps are Hard
iamctodd
PRO
55
11k
BBQ
matthewcrist
89
9.8k
Designing for Performance
lara
610
69k
Producing Creativity
orderedlist
PRO
347
40k
Building an army of robots
kneath
306
46k
The Cult of Friendly URLs
andyhume
79
6.6k
Bash Introduction
62gerente
615
210k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.2k
The Invisible Side of Design
smashingmag
302
51k
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!