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
310
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
92
UI Synchronicity
eliperelman
1
100
Other Decks in Technology
See All in Technology
From here to resilience - a travel guide
ufried
1
160
令和版ソフトウェアエンジニアの情報収集術 PHPカンファレンス香川2024
ysknsid25
4
880
TiDBにおけるテーブル設計と最適化の事例
cygames
0
790
OPENLOGI Company Profile for engineer
hr01
1
2.2k
QA経験のないエンジニアリング マネージャーがQAのカジュアル面談に出て 苦労していること・気づいたこと / scrum fest niigata 2024
yoshikiiida
2
660
株式会社EventHub・エンジニア採用資料
eventhub
0
2.1k
Taking Flight with Tailwind CSS
opdavies
0
4.3k
Dungeons and Dragons and Rails
joelq
0
230
Google Cloud Next '24 Recap in ZOZO AIにより変わる開発 運用/Development and operation changed by AI
gachimuchiengineer
0
190
RubyKaigi 2024 - Make Your Own Regex Engine!
makenowjust
0
150
ハードウェアを動かすTypeScriptの世界
9wick
3
1.2k
テストコードを書きながらCompose Multiplatformを乗りこなす
subroh0508
0
140
Featured
See All Featured
The Straight Up "How To Draw Better" Workshop
denniskardys
228
130k
Web Components: a chance to create the future
zenorocha
306
41k
Become a Pro
speakerdeck
PRO
13
4.6k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
228
16k
Navigating Team Friction
lara
179
13k
A better future with KSS
kneath
231
16k
Art, The Web, and Tiny UX
lynnandtonic
290
19k
Building Applications with DynamoDB
mza
88
5.7k
YesSQL, Process and Tooling at Scale
rocio
165
13k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
18
7k
Producing Creativity
orderedlist
PRO
338
39k
Making Projects Easy
brettharned
109
5.5k
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!