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
340
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
110
UI Synchronicity
eliperelman
1
120
Other Decks in Technology
See All in Technology
AIは脅威でなくチャンス。 AIと共に進化するエンジニアの成長戦略 / geeksai-2025-spring
carta_engineering
0
280
貧民的プログラミングのすすめ
kakehashi
PRO
2
370
Go Modulesの仕組み Bundler(Ruby)との比較を添えて
daisuketakeda
0
1.8k
Platform Engineeringで クラウドの「楽しくない」を解消しよう
jacopen
5
340
いまから始めるAWS CDK 〜モダンなインフラ構築入門〜/iac-night-cdk-introduction
tomoki10
6
1.9k
Real World Nix CI/CD編
asa1984
1
200
Db2 SaaS(Db2 on Cloud Gen3)を見てみよう/20250306-Db2SaaS-dojo
mayumihirano
0
100
「頑張る」を「楽しむ」に変換する技術
tomoyakitaura
14
7.3k
一歩ずつ成長しながら進める ZOZOの基幹システムリプレイス/Growing Stap by Stap ZOZO BackOffice System Replacement
cocet33000
2
500
完璧を捨てろ! “攻め”のQAがもたらすスピードと革新/20250306 Hiroki Hachisuka
shift_evolve
0
200
Cursorで学ぶAIエディター / understand-ai-editor-by-cursor
shuzon
0
150
Kubernetesを手元で学ぼう! 初心者向けローカル環境のススメ
nayaaaa
PRO
2
690
Featured
See All Featured
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
7
680
Thoughts on Productivity
jonyablonski
69
4.5k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
29
1.1k
Building Adaptive Systems
keathley
40
2.4k
Raft: Consensus for Rubyists
vanstee
137
6.8k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.2k
Building Flexible Design Systems
yeseniaperezcruz
328
38k
Speed Design
sergeychernyshev
28
830
Dealing with People You Can't Stand - Big Design 2015
cassininazir
366
25k
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!