Terry Jones and Nicholas H. Tollervey
Learning jQuery Deferreds
Learning jQuery Deferreds
by Terry Jones and Nicholas H. Tollervey
Copyright © 2014 Terry Jones and Nicholas H. Tollervey. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/
institutional sales department: 800-998-9938 or corporate@oreilly.com.
Editors: Simon St. Laurent and Meghan Blanchette
Production Editor: Kristen Brown
Copyeditor: Charles Roumeliotis
Proofreader: Kristen Brown
Cover Designer: Karen Montgomery
Interior Designer: David Futato
January 2014: First Edition
Revision History for the First Edition:
2013-12-20: First release
See http://oreilly.com/catalog/errata.csp?isbn=9781449369392 for release details.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly
Media, Inc. Learning jQuery Deferreds, the image of a musky rat-kangaroo, and related trade dress are
trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trade‐
mark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume
no responsibility for errors or omissions, or for damages resulting from the use of the information contained
herein.
ISBN: 978-1-449-36939-2
[LSI]
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii
1.
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Food for Thought 1
Terminology: Deferreds and Promises 3
Familiar Promises 4
2.
The jQuery Deferred API. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Consuming Promises 8
More Terminology: Resolve, Reject and Progress 8
done 9
fail 9
always 10
progress 10
promise 11
then 11
state 13
when 13
Creating Deferreds 15
Construction 15
resolve and resolveWith 16
reject and rejectWith 17
notify and notifyWith 17
Putting It All Together 17
Deferred Dynamics 18
Deprecated Promise Methods 19
isRejected and isResolved 19
pipe 19
iii