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
310
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
97
UI Synchronicity
eliperelman
1
110
Other Decks in Technology
See All in Technology
Shopifyアプリ開発における Shopifyの機能活用
sonatard
4
210
地理情報データをデータベースに格納しよう~ GPUを活用した爆速データベース PG-Stromの紹介 ~
sakaik
1
130
リンクアンドモチベーション ソフトウェアエンジニア向け紹介資料 / Introduction to Link and Motivation for Software Engineers
lmi
4
300k
Redmine 6.0 新機能評価ガイド
vividtone
0
310
AWS Lambdaと歩んだ“サーバーレス”と今後 #lambda_10years
yoshidashingo
1
120
SREの組織類型に応じた リーダシップの考察
kenta_hi
PRO
1
640
TinyGoを使ったVSCode拡張機能実装
askua
2
210
第23回Ques_タイミーにおけるQAチームの在り方 / QA Team in Timee
takeyaqa
0
260
State of Open Source Web Mapping Libraries
dayjournal
0
230
Railsで4GBのデカ動画ファイルのアップロードと配信、どう実現する?
asflash8
2
270
Intuneお役立ちツールのご紹介
sukank
3
770
Engineering at LY Corporation
lycorp_recruit_jp
0
590
Featured
See All Featured
Designing the Hi-DPI Web
ddemaree
280
34k
Speed Design
sergeychernyshev
24
610
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.3k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
43
2.2k
GraphQLとの向き合い方2022年版
quramy
43
13k
Music & Morning Musume
bryan
46
6.2k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
7
570
Become a Pro
speakerdeck
PRO
25
5k
What's in a price? How to price your products and services
michaelherold
243
12k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Facilitating Awesome Meetings
lara
50
6.1k
Adopting Sorbet at Scale
ufuk
73
9.1k
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!