Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
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
370
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
120
UI Synchronicity
eliperelman
1
140
Other Decks in Technology
See All in Technology
事業の財務責任に向き合うリクルートデータプラットフォームのFinOps
recruitengineers
PRO
2
200
日本Rubyの会: これまでとこれから
snoozer05
PRO
5
230
ハッカソンから社内プロダクトへ AIエージェント「ko☆shi」開発で学んだ4つの重要要素
sonoda_mj
6
1.6k
ESXi のAIOps だ!2025冬
unnowataru
0
350
SQLだけでマイグレーションしたい!
makki_d
0
1.2k
AWSインフルエンサーへの道 / load of AWS Influencer
whisaiyo
0
220
Lookerで実現するセキュアな外部データ提供
zozotech
PRO
0
200
「図面」から「法則」へ 〜メタ視点で読み解く現代のソフトウェアアーキテクチャ〜
scova0731
0
490
AWSに革命を起こすかもしれない新サービス・アップデートについてのお話
yama3133
0
500
TED_modeki_共創ラボ_20251203.pdf
iotcomjpadmin
0
150
ExpoのインダストリーブースでみたAWSが見せる製造業の未来
hamadakoji
0
190
ソフトウェアエンジニアとAIエンジニアの役割分担についてのある事例
kworkdev
PRO
0
220
Featured
See All Featured
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
0
22
Technical Leadership for Architectural Decision Making
baasie
0
180
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
400
Color Theory Basics | Prateek | Gurzu
gurzu
0
150
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
0
3.4k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.3k
How to Talk to Developers About Accessibility
jct
1
85
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
34
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
0
120
End of SEO as We Know It (SMX Advanced Version)
ipullrank
2
3.8k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Raft: Consensus for Rubyists
vanstee
141
7.3k
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!