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
320
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
110
Other Decks in Technology
See All in Technology
SREとしてスタッフエンジニアを目指す / SRE Kaigi 2025
tjun
11
3k
一人から始めたSREチーム3年の歩み - 求められるスキルの変化とチームのあり方 - / The three-year journey of the SRE team, which started all by myself
vtryo
5
2.4k
PaaSの歴史と、 アプリケーションプラットフォームのこれから
jacopen
7
1.7k
embedパッケージを深掘りする / Deep Dive into embed Package in Go
task4233
1
240
【NGK2025S】動物園(PINTO_model_zoo)に遊びに行こう
kazuhitotakahashi
0
340
Amazon Route 53, 待ちに待った TLSAレコードのサポート開始
kenichinakamura
0
200
[SRE kaigi 2025] ガバメントクラウドに向けた開発と変化するSRE組織のあり方 / Development for Government Cloud and the Evolving Role of SRE Teams
kazeburo
3
1k
FODにおけるホーム画面編成のレコメンド
watarukudo
PRO
2
410
Building Scalable Backend Services with Firebase
wisdommatt
0
110
いま現場PMのあなたが、 経営と向き合うPMになるために 必要なこと、腹をくくること
hiro93n
9
8.6k
DMMブックスへのTipKit導入
ttyi2
1
140
横断SREの立ち上げと、AWSセキュリティへの取り組みの軌跡
rvirus0817
3
1.9k
Featured
See All Featured
A Modern Web Designer's Workflow
chriscoyier
693
190k
Side Projects
sachag
452
42k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
4
190
Unsuck your backbone
ammeep
669
57k
GraphQLの誤解/rethinking-graphql
sonatard
68
10k
Optimising Largest Contentful Paint
csswizardry
33
3k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
880
Why Our Code Smells
bkeepers
PRO
335
57k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
33
2.7k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
27
1.5k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.2k
Speed Design
sergeychernyshev
25
740
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!