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
300
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
91
UI Synchronicity
eliperelman
1
100
Other Decks in Technology
See All in Technology
Android Target SDK 35 (Android 15) 対応の概要
akkie76
0
160
Azureの基本的な権限管理の勉強会
yhana
1
2.2k
MLOpsの「壁」を乗り越える、LINEヤフーの Data Quality as Code
lycorptech_jp
PRO
8
650
Gitlab本から学んだこと - そーだいなるプレイバック / gitlab-book
soudai
7
1.4k
Tellus の衛星データを見てみよう #mf_fukuoka
kongmingstrap
0
320
Handling focus in 2024
tahia910
0
250
Gradle Build Scanを使ってビルドのことを知ろう potatotips #87
tomorrowkey
2
160
IaCからAWSに入門した初心者が CloudFormationを通して考えた「AWS操作」の使い分け
maimyyym
2
530
Building a RAG-poweredAI chat appwith Python and VS Code
pamelafox
0
160
IaCジェネレーターとBedrockで詳細設計書を生成してみた
tsukasa_ishimaru
4
920
Microsoft for Startups Founders Hub_20240429 update
daikikanemitsu
1
2.4k
Building Dashboards as a Hobby
egmc
0
400
Featured
See All Featured
The MySQL Ecosystem @ GitHub 2015
samlambert
244
12k
ParisWeb 2013: Learning to Love: Crash Course in Emotional UX Design
dotmariusz
104
6.6k
The Language of Interfaces
destraynor
151
23k
Done Done
chrislema
178
15k
Designing on Purpose - Digital PM Summit 2013
jponch
111
6.5k
Making Projects Easy
brettharned
109
5.5k
What's new in Ruby 2.0
geeforr
337
31k
Happy Clients
brianwarren
92
6.4k
The Power of CSS Pseudo Elements
geoffreycrofte
62
5k
How STYLIGHT went responsive
nonsquared
92
4.8k
Facilitating Awesome Meetings
lara
43
5.6k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
22
1.6k
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!