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
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
130
Other Decks in Technology
See All in Technology
re:Invent完全攻略ガイド
junjikoide
1
270
メタプログラミングRuby問題集の活用
willnet
2
780
Rubyist入門: The Way to The Timeless Way of Programming
snoozer05
PRO
5
310
Data & AIの未来とLakeHouse
ishikawa_satoru
0
720
フライトコントローラPX4の中身(制御器)を覗いてみた
santana_hammer
1
140
AIを前提に、業務を”再構築”せよ IVRyの9ヶ月にわたる挑戦と未来の働き方 (BTCONJP2025)
yueda256
1
140
AIエージェントは「使う」だけじゃなくて「作る」時代! 〜最新フレームワークで楽しく開発入門しよう〜
minorun365
11
1.6k
こんな時代だからこそ! 想定しておきたいアクセスキー漏洩後のムーブ
takuyay0ne
4
540
Design and implementation of "Markdown to Google Slides" / phpconfuk 2025
k1low
1
390
QAセントラル組織が運営する自動テストプラットフォームの課題と現状
lycorptech_jp
PRO
0
370
"おまじない"はもう卒業! デバッガで探るSpring Bootの裏側と「学び方」の学び方
takeuchi_132917
0
110
us-east-1 の障害が 起きると なぜ ソワソワするのか
miu_crescent
PRO
2
800
Featured
See All Featured
Being A Developer After 40
akosma
91
590k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.7k
Docker and Python
trallard
46
3.6k
Fireside Chat
paigeccino
41
3.7k
Why You Should Never Use an ORM
jnunemaker
PRO
60
9.6k
A Modern Web Designer's Workflow
chriscoyier
697
190k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
1.1k
The Cost Of JavaScript in 2023
addyosmani
55
9.2k
Raft: Consensus for Rubyists
vanstee
140
7.2k
Testing 201, or: Great Expectations
jmmastey
46
7.8k
The Power of CSS Pseudo Elements
geoffreycrofte
80
6.1k
Imperfection Machines: The Place of Print at Facebook
scottboms
269
13k
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!