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
380
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
130
UI Synchronicity
eliperelman
1
140
Other Decks in Technology
See All in Technology
kintone開発のプラットフォームエンジニアの紹介
cybozuinsideout
PRO
0
560
Bill One 開発エンジニア 紹介資料
sansan33
PRO
4
17k
多様な最適化サービス開発をスケールさせる共通基盤とチーム構成
algoartis
0
110
Kiro Power - Amazon Bedrock AgentCore を学ぶ、もう一つの方法
r3_yamauchi
PRO
0
110
Oracle Database@Azure:サービス概要のご紹介
oracle4engineer
PRO
3
420
#22 CA × atmaCup 3rd 1st Place Solution
yumizu
1
230
Data Intelligence on Lakehouse Paradigm
scotthsieh825
0
180
たかがボタン、されどボタン ~button要素から深ぼるボタンUIの定義について~ / BuriKaigi 2026
yamanoku
1
290
アウトプットはいいぞ / output_iizo
uhooi
0
140
BPaaSオペレーション・kubell社内 n8n活用による効率化検証事例紹介
kubell_hr
0
120
困ったCSVファイルの話
mottyzzz
0
340
Security Hub と出会ってから 1年半が過ぎました
rch850
0
170
Featured
See All Featured
Balancing Empowerment & Direction
lara
5
850
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
67
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
1
38
What does AI have to do with Human Rights?
axbom
PRO
0
1.9k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
44
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.1k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
560
Navigating Team Friction
lara
192
16k
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
1
74
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
340
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!