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
130
Other Decks in Technology
See All in Technology
ガチな登山用デバイスからこんにちは
halka
1
230
【実演版】カンファレンス登壇者・スタッフにこそ知ってほしいマイクの使い方 / 大吉祥寺.pm 2025
arthur1
1
670
Webブラウザ向け動画配信プレイヤーの 大規模リプレイスから得た知見と学び
yud0uhu
0
230
Obsidian応用活用術
onikun94
1
450
Skrub: machine-learning with dataframes
gaelvaroquaux
0
120
AI時代に非連続な成長を実現するエンジニアリング戦略
sansantech
PRO
3
1.2k
RSCの時代にReactとフレームワークの境界を探る
uhyo
10
3.3k
生成AIでセキュリティ運用を効率化する話
sakaitakeshi
0
480
これでもう迷わない!Jetpack Composeの書き方実践ガイド
zozotech
PRO
0
290
Rustから学ぶ 非同期処理の仕組み
skanehira
1
130
生成AI時代のデータ基盤設計〜ペースレイヤリングで実現する高速開発と持続性〜 / Levtech Meetup_Session_2
sansan_randd
1
150
初めてAWSを使うときのセキュリティ覚書〜初心者支部編〜
cmusudakeisuke
1
220
Featured
See All Featured
KATA
mclloyd
32
14k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.9k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
Making the Leap to Tech Lead
cromwellryan
135
9.5k
Automating Front-end Workflow
addyosmani
1370
200k
Speed Design
sergeychernyshev
32
1.1k
Raft: Consensus for Rubyists
vanstee
140
7.1k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Become a Pro
speakerdeck
PRO
29
5.5k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.4k
Visualization
eitanlees
148
16k
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!