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
390
10
Share
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
More Decks by Eli Perelman
See All by Eli Perelman
Intro to Raptor
eliperelman
0
140
UI Synchronicity
eliperelman
1
150
Other Decks in Technology
See All in Technology
目的ファーストのハーネス設計 ~ハーネスの変更容易性を高めるための優先順位~
gotalab555
8
2.5k
AIが書いたコードを信じられない問題 〜レビュー負荷を下げるために変えたこと〜 / The AI Code Trust Gap: Reducing the Review Burden
bitkey
PRO
8
1.4k
AzureのIaC管理からログ調査まで、随所に役立つSkillsとCustom-Instructions / Boosting IaC and Log Analysis with Skills
aeonpeople
0
280
[最強DB講義]推薦システム | 評価編
recsyslab
PRO
0
110
AWS Agent Registry の基礎・概要を理解する/aws-agent-registry-intro
ren8k
3
410
巨大プラットフォームを進化させる「第3のROI」
recruitengineers
PRO
2
1.4k
Class.new is all you need
riseshia
1
190
運用システムにおけるデータ活用とPlatform
sansantech
PRO
0
130
PicoRuby as a Multi-VM Operating System
kishima
1
220
AIが自律的に働く時代へ Amazon Quick で実現するAIエージェント紹介
koheiyoshikawa
0
140
AI時代のガードレールとしてのAPIガバナンス
nagix
0
310
VespaのParent Childを用いたフィードパフォーマンスの改善
taking
0
120
Featured
See All Featured
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.9k
Darren the Foodie - Storyboard
khoart
PRO
3
3.3k
VelocityConf: Rendering Performance Case Studies
addyosmani
333
25k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
340
Embracing the Ebb and Flow
colly
88
5k
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
180
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
230
Fireside Chat
paigeccino
42
3.9k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
350
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
130
Visualization
eitanlees
150
17k
Code Review Best Practice
trishagee
74
20k
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!