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
90
UI Synchronicity
eliperelman
1
99
Other Decks in Technology
See All in Technology
Autify Company Deck
autifyhq
1
30k
統計的学習理論読み Chapter 1
kmatsui
3
900
Getting started with controlling LEGO using Swift
hcrane
0
130
私のRSpecの書き方 / How I write RSpec
tmtms
4
830
OpenTelemetry実践 はじめの一歩
taxin
0
320
検証からプロダクトへ: シームレスなLLM開発の ためのしくみ作り
nunukim
1
190
技術広報経験0のEMがエンジニアブランディングをはじめてみた
coconala_engineer
1
130
マイクロサービス環境におけるDB戦略 in DMMプラットフォーム
pospome
11
3k
継続的テストモデルを実現するためにスリーアミーゴスを用いた10Xでのシフトレフトの事例
nihonbuson
3
220
プッシュ型子育てサービスを、先行プロジェクト実施自治体において開始します
govtechtokyo
0
270
XRミーティング 2024-03-20
1ftseabass
PRO
0
100
エンジニア候補者向け資料2024.03.28.pdf
macloud
0
2.9k
Featured
See All Featured
Documentation Writing (for coders)
carmenintech
59
3.8k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
67
38k
Reflections from 52 weeks, 52 projects
jeffersonlam
343
19k
Clear Off the Table
cherdarchuk
82
310k
Embracing the Ebb and Flow
colly
78
4.1k
Bash Introduction
62gerente
604
210k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
153
14k
Code Reviewing Like a Champion
maltzj
512
39k
Being A Developer After 40
akosma
56
580k
Rebuilding a faster, lazier Slack
samanthasiow
72
8.2k
Infographics Made Easy
chrislema
237
18k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
18
6.8k
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!