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
150
Other Decks in Technology
See All in Technology
3つのボトルネックを解消し、リリースエンジニアリングを再定義した話
nealle
0
200
Oracle AI Databaseデータベース・サービス: BaseDB/ExaDB-Dの可用性
oracle4engineer
PRO
1
150
最大のアウトプット術は問題を作ること
ryoaccount
0
320
OPENLOGI Company Profile
hr01
0
83k
ストライクウィッチーズ2期6話のエイラの行動が許せないのでPjMの観点から何をすべきだったのかを考える
ichimichi
1
290
建設的な現実逃避のしかた / How to practice constructive escapism
pauli
4
290
AIがコードを書く時代の ジェネレーティブプログラミング
polidog
PRO
3
610
プロダクトを育てるように生成AIによる開発プロセスを育てよう
kakehashi
PRO
1
850
さくらのクラウドでつくるCloudNative Daysのオブザーバビリティ基盤
b1gb4by
0
110
Babylon.js を使って試した色々な内容 / Various things I tried using Babylon.js / Babylon.js 勉強会 vol.5
you
PRO
0
260
あるアーキテクチャ決定と その結果/architecture-decision-and-its-result
hanhan1978
2
540
ふりかえりを 「あそび」にしたら、 学習が勝手に進んだ / Playful Retros Drive Learning
katoaz
0
390
Featured
See All Featured
Evolving SEO for Evolving Search Engines
ryanjones
0
170
The Curious Case for Waylosing
cassininazir
0
290
Embracing the Ebb and Flow
colly
88
5k
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
1
500
The Mindset for Success: Future Career Progression
greggifford
PRO
0
300
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
170
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
410
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
200
Designing for Performance
lara
611
70k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
3.8k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
110
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!