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
360
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
120
UI Synchronicity
eliperelman
1
120
Other Decks in Technology
See All in Technology
フルカイテン株式会社 エンジニア向け採用資料
fullkaiten
0
6.9k
Kotlinで学ぶ 代数的データ型
ysknsid25
5
820
FASTと向き合うことで見えた、大規模アジャイルの難しさと楽しさ
wooootack
0
410
Kubernetesで作るAIプラットフォーム
oracle4engineer
PRO
2
190
AIとSREの未来 / AI and SRE
ymotongpoo
2
1.9k
CSSDay, Amsterdam
brucel
0
290
現場で役立つAPIデザイン
nagix
1
210
Amazon DevOps Guru のベースラインを整備して1ヶ月ほど運用してみた #jawsug_asa / Amazon DevOps Guru trial
masahirokawahara
3
220
ai bot got sick (abc 2025s version)
kojira
0
150
開発効率と信頼性を両立する Ubieのプラットフォームエンジニアリング
teru0x1
0
110
AWS Lambdaでサーバレス設計を学ぼう_ベンダーロックインの懸念を超えて-サーバレスの真価を探る
fukuchiiinu
4
950
Nonaka Sensei
kawaguti
PRO
3
510
Featured
See All Featured
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
26k
Product Roadmaps are Hard
iamctodd
PRO
53
11k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
130
19k
Speed Design
sergeychernyshev
30
980
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
331
22k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
31
1.2k
Optimizing for Happiness
mojombo
379
70k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
123
52k
Site-Speed That Sticks
csswizardry
9
620
Optimising Largest Contentful Paint
csswizardry
37
3.3k
Stop Working from a Prison Cell
hatefulcrawdad
269
20k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
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!