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
92
UI Synchronicity
eliperelman
1
100
Other Decks in Technology
See All in Technology
TiDBにおけるテーブル設計と最適化の事例
cygames
0
760
テストコードを書きながらCompose Multiplatformを乗りこなす
subroh0508
0
130
Dungeons and Dragons and Rails
joelq
0
220
Deno で作る快適な “as Code” プラットフォーム – TSKaigi 2024
pizzacat83
4
270
Step by Stepで学ぶ、ADT(代数的データ型)、モナドからEffect-TSまで
leveragestech
1
2.5k
大規模言語モデル (LLM)における低精度数値表現
pfn
PRO
3
760
TypescriptでのContextualな構造化ロギングと社内全体への導入
leveragestech
3
440
Cloudflare WorkersがPythonに対応したので試してみた
miura55
0
180
本当のガバクラ基礎
toru_kubota
0
300
Documentação de Produtos: Artefatos essenciais na prática
rigolon
1
280
株式会社EventHub・エンジニア採用資料
eventhub
0
2.1k
5分で分かる(かもしれない) Vector engine for OpenSearch Serverless
tsukuboshi
1
350
Featured
See All Featured
WebSockets: Embracing the real-time Web
robhawkes
59
7k
Bootstrapping a Software Product
garrettdimon
PRO
302
110k
What’s in a name? Adding method to the madness
productmarketing
PRO
17
2.7k
Facilitating Awesome Meetings
lara
43
5.6k
Reflections from 52 weeks, 52 projects
jeffersonlam
345
19k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
275
13k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
21
1.6k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
226
51k
Building Your Own Lightsaber
phodgson
100
5.7k
[RailsConf 2023] Rails as a piece of cake
palkan
28
4k
What's in a price? How to price your products and services
michaelherold
238
11k
The Invisible Side of Design
smashingmag
294
49k
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!