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
Pythonデータ分析実践試験 出題傾向や学習のポイントとテクニカルハイライト
terapyon
1
110
Perl歴約10年のエンジニアがフルスタックTypeScriptに出会ってみた
papix
1
260
AIエージェント開発手法と業務導入のプラクティス
ykosaka
9
2.7k
今日からはじめるプラットフォームエンジニアリング
jacopen
8
1.9k
Microsoft Fabric vs Databricks vs (Snowflake) -若手エンジニアがそれぞれの強みと違いを比較してみた- "A Young Engineer's Comparison of Their Strengths and Differences"
reireireijinjin6
1
130
Как мы автоматизировали интеграционное тестирование с Gonkey и не пожалели. Паша Егорычев, Кирилл Поляков
lamodatech
0
1.6k
Gateway H2 モジュールで スマートホーム入門
minoruinachi
0
120
Notion x ポストモーテムで広げる組織の学び / Notion x Postmortem
isaoshimizu
1
150
白金鉱業Meetup_Vol.18_生成AIはデータサイエンティストを代替するのか?
brainpadpr
4
220
QA/SDETの現在と、これからの挑戦
imtnd
0
220
OPENLOGI Company Profile for engineer
hr01
1
25k
10分で学ぶ、RAGの仕組みと実践
supermarimobros
0
730
Featured
See All Featured
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
26k
The Cult of Friendly URLs
andyhume
78
6.3k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
160
15k
Gamification - CAS2011
davidbonilla
81
5.3k
Designing for humans not robots
tammielis
253
25k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
34
2.2k
Building a Modern Day E-commerce SEO Strategy
aleyda
40
7.2k
Practical Orchestrator
shlominoach
187
11k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
120k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
23
2.7k
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!