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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Eli Perelman
November 01, 2011
Technology
390
10
Share
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
More Decks by Eli Perelman
See All by Eli Perelman
Intro to Raptor
eliperelman
0
140
UI Synchronicity
eliperelman
1
160
Other Decks in Technology
See All in Technology
layerx-fde-practices
cipepser
6
2.5k
データ基盤構築・運用の現場から 〜 Snowflake Intelligence 導入で変わった、データ活用の未来 〜
wonohe
0
160
ANDPAD Ruby sponsor session in RubyKaigi 2026
andpad
0
130
AsyncStreamでマルチブロードキャストを実装する
1mash0
1
220
Sansan Engineering Unit 紹介資料
sansan33
PRO
1
4.5k
freee-mcpを Local→Remote で出してわかった MCP認可実装のリアル
terara
2
500
JaSSTに関わることで変わった人生観 #jasstnano
makky_tyuyan
0
180
既存プロダクトQAから新規プロダクトQAへ
ryotakahashi
0
180
checker.tsにチキンレースを仕掛けてみた:型エラー(TS2589)が発生する境界線を求めて
hal_spidernight
1
170
AI全盛の今だからこそ、あえてもう一度振り返るAPIの基礎
smt7174
3
160
責任あるソフトウェアエンジニアリングの紹介4章・5章 / RSE_Ch4-5
ido_kara_deru
0
290
シンデレラなんかになりたくない!ガラスの靴が割れた時代にどう歩く?
nomizone
0
160
Featured
See All Featured
Leading Effective Engineering Teams in the AI Era
addyosmani
9
1.9k
A Tale of Four Properties
chriscoyier
163
24k
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
230
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
150
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.2k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
170
So, you think you're a good person
axbom
PRO
2
2k
Why Our Code Smells
bkeepers
PRO
340
58k
Agile that works and the tools we love
rasmusluckow
331
21k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
240
[SF Ruby Conf 2025] Rails X
palkan
2
1k
SEO for Brand Visibility & Recognition
aleyda
0
4.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!