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
390
10
Share
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
More Decks by Eli Perelman
See All by Eli Perelman
Intro to Raptor
eliperelman
0
140
UI Synchronicity
eliperelman
1
150
Other Decks in Technology
See All in Technology
バイブコーディングで3倍早く⚪⚪を作ってみた
samakada
0
180
目的ファーストのハーネス設計 ~ハーネスの変更容易性を高めるための優先順位~
gotalab555
8
2.5k
運用システムにおけるデータ活用とPlatform
sansantech
PRO
0
130
ハーネスエンジニアリングをやりすぎた話 ~そのハーネスは解体された~
gotalab555
5
1.9k
Good Enough Types: Heuristic Type Inference for Ruby
riseshia
1
320
ハーネスエンジニアリングの概要と設計思想
sergicalsix
9
5.5k
Cortex Codeのコスト見積ヒントご紹介
yokatsuki
0
120
AWS Transform CustomでIaCコードを自由自在に変換しよう
duelist2020jp
0
160
Route 53 Global Resolver で高額課金発生!
otanikohei2023
0
130
データを"持てない"環境でのアノテーション基盤設計
sansantech
PRO
1
150
「SaaSの次の時代」に重要性を増すステークホルダーマネジメントの要諦 ~解像度を圧倒的に高めPdMの価値を最大化させる方法~
kakehashi
PRO
3
2.7k
AI時代における技術的負債への取り組み
codenote
1
1.8k
Featured
See All Featured
Are puppies a ranking factor?
jonoalderson
1
3.3k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
64
54k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Side Projects
sachag
455
43k
The Pragmatic Product Professional
lauravandoore
37
7.2k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.3k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
What's in a price? How to price your products and services
michaelherold
247
13k
sira's awesome portfolio website redesign presentation
elsirapls
0
220
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
118
110k
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!