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
94
UI Synchronicity
eliperelman
1
100
Other Decks in Technology
See All in Technology
開発生産性をむしろ向上させる セキュリティパートナーの作り方 / Dev Productivity Con 2024
flatt_security
0
390
エンジニアの生存戦略 〜クラウド潮流の経験から紐解く技術トレンドのメカニズムと乗りこなし方〜
shimy
9
1.9k
[I/O Extended Android 2024] What`s new in Android 2024
kyeongwan
0
220
目標設定は好きですか? アジャイルとともに目標と向き合い続ける方法 / Do you like target Management?
kakehashi
10
3k
Matterport を使ってクラスメソッド各拠点のバーチャルオフィスツアーを作成してみた
wakatsuki
0
160
OSSコミットしてZennの課題を解決した話
dyoshikawa1993
0
150
AWSサービスメニュー開発をしていてAWSを好きだ!と感じた瞬間
toru_kubota
0
130
DevIO2024_レガシー運用からの脱却 -クラウド活用の実践事例とベストプラクティス-
jun2882
0
210
「我々はどこに向かっているのか」を問い続けるための仕組みづくり / Establishing a System for Continuous Inquiry about where we are
daitasu
0
170
ペパボのオブザーバビリティ研修2024 説明資料
kesompochy
0
1.1k
公共領域から学ぶ クラウド移行についてエンジニアが意識していること
kawakawa2222
0
140
Azure AI ことはじめ
tsubakimoto_s
0
130
Featured
See All Featured
What's in a price? How to price your products and services
michaelherold
239
11k
Practical Orchestrator
shlominoach
185
10k
Fontdeck: Realign not Redesign
paulrobertlloyd
79
5.1k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
78
15k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
20
7.2k
How GitHub (no longer) Works
holman
305
140k
How STYLIGHT went responsive
nonsquared
93
5k
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
The Mythical Team-Month
searls
217
43k
Principles of Awesome APIs and How to Build Them.
keavy
124
16k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
226
52k
Build your cross-platform service in a week with App Engine
jlugia
227
17k
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!