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
360
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
120
Other Decks in Technology
See All in Technology
「規約、知識、オペレーション」から考える中規模以上の開発組織のCursorルールの 考え方・育て方 / Cursor Rules for Coding Styles, Domain Knowledges and Operations
yuitosato
6
1.6k
QAはソフトウェアエンジニアリングを学んで実践するのが大事なの
ymty
1
370
(非公式) AWS Summit Japan と 海浜幕張 の歩き方 2025年版
coosuke
PRO
0
140
TerraformをSaaSで使うとAzureの運用がこんなに楽ちん!HCP Terraformって何?
mnakabayashi
0
110
産業機械をElixirで制御する
kikuyuta
0
160
Securing your Lambda 101
chillzprezi
0
240
Digitization部 紹介資料
sansan33
PRO
1
4.2k
フルカイテン株式会社 エンジニア向け採用資料
fullkaiten
0
7.2k
Create a Rails8 responsive app with Gemini and RubyLLM
palladius
0
110
IAMのマニアックな話 2025を執筆して、 見えてきたAWSアカウント管理の現在
nrinetcom
PRO
3
520
型システムを知りたい人のための型検査器作成入門
mame
14
3.6k
Copilot Agentを普段使いしてわかった、バックエンド開発で使えるTips
ykagano
0
210
Featured
See All Featured
Making Projects Easy
brettharned
116
6.2k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
6
690
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
14
1.5k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
180
53k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
8
650
Product Roadmaps are Hard
iamctodd
PRO
53
11k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.6k
What's in a price? How to price your products and services
michaelherold
245
12k
Designing Experiences People Love
moore
142
24k
GraphQLの誤解/rethinking-graphql
sonatard
71
11k
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!