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
300
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
91
UI Synchronicity
eliperelman
1
100
Other Decks in Technology
See All in Technology
GraphQL 成熟度モデルの紹介と、プロダクトに当てはめた事例 / GraphQL maturity model
mh4gf
7
1.3k
Azure Container Apps + Bicep 〜 こんな感じで運用しています
kaz29
2
450
ChatworkのSRE部って実は 半分くらいPlatform Engineering部かもしれない
saramune
0
160
地理空間データ可視化・解析・活用ソリューション Pacific Spatial Solutions (PSS)
pacificspatialsolutions
0
140
[新卒向け研修資料] テスト文字列に「うんこ」と入れるな(2024年版)
infiniteloop_inc
3
12k
DevOpsDays History and my DevOps story
kawaguti
PRO
9
2.5k
Kernel MemoryでAzure OpenAI Serviceとお手軽データソース連携
mitsuzono
1
240
VSCodeの拡張機能を作っている話
ebarakazuhiro
1
320
リテール金融(キャッシュレス・ネット銀行・ネット証券)の競争環境と経済圏
8maki
0
560
Azure犬駆動開発の記録/GlobalAzureFukuoka2024_20240420
nina01
1
210
最近たまに見かけるTiDBってなんだ? - Findy
pingcap0315
2
770
よく聞くけど使ったことないソフトウェアNo.1 KafkaとSnowflake
foursue
4
350
Featured
See All Featured
YesSQL, Process and Tooling at Scale
rocio
164
13k
How STYLIGHT went responsive
nonsquared
92
4.8k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
221
21k
5 minutes of I Can Smell Your CMS
philhawksworth
199
19k
GraphQLの誤解/rethinking-graphql
sonatard
50
9.2k
Embracing the Ebb and Flow
colly
80
4.1k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
60
14k
jQuery: Nuts, Bolts and Bling
dougneiner
59
7.1k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
40
4.4k
10 Git Anti Patterns You Should be Aware of
lemiorhan
648
58k
The Power of CSS Pseudo Elements
geoffreycrofte
60
5k
How to train your dragon (web standard)
notwaldorf
73
5.2k
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!