Async & Performance
by Kyle Simpson
Copyright 2015 Getify Solutions, Inc. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safaribooksonline.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Editors: Simon St. Laurent and
Brian MacDonald - Production Editor: Kristen Brown
- Copyeditor: Jasmine Kwityn
- Proofreader: Phil Dangler
- Interior Designer: David Futato
- Cover Designer: Ellie Volckhausen
- March 2015: First Edition
Revision History for the First Edition
- 2015-02-19: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781491904220 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. You Dont Know JS: Async & Performance, the cover image, and related trade dress are trademarks of OReilly Media, Inc.
While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
978-1-491-90422-0
[LSI]
Foreword
Over the years, my employer has trusted me enough to conduct interviews.If were looking for someone with JavaScript skills, my first line ofquestioningwell, actually, is to check if the candidateneeds the bathroom and/or a drink, because comfort is important. Butonce Im past the bit about the candidates fluid intake/output, I setabout determining if the candidate knows JavaScript, or just jQuery.
Not that theres anything wrong with jQuery. It lets you do a lotwithout really knowing JavaScript, and thats a featurenot a bug. Butif the job calls for advanced skills in JavaScript performance andmaintainability, you need someone who knows how libraries such as jQueryare put together. You need to be able to harness the core of JavaScriptthe same way they do.
If I want to get a picture of someones core JavaScript skill, Im mostinterested in what they make of closures (youve read the You Dont Know JS: Scope & Closures title of thisseries already, right?) and how to get the most out of asynchronicity,which brings us to this book.
For starters, youll be taken through callbacks, the bread and butter ofasynchronous programming. Of course, bread and butter does not make fora particularly satisfying meal, but the next course is full of tasty,tasty Promises!
If you dont know Promises, now is the time to learn. Promises are nowthe official way to provide async return values in both JavaScript andthe DOM. All future async DOM APIs will use them, and many already do, so beprepared! At the time of writing, Promises have shipped in most majorbrowsers, with IE shipping soon. Once youve finished savoring Promises, I hope youleft room for the next course, Generators.
Generators snuck their way into stable versions of Chrome and Firefoxwithout too much pomp and ceremony, because, frankly, theyre morecomplicated than they are interesting. Or, thats what I thoughtuntil I saw them combined with Promises. There, they become an importanttool in readability and maintenance.
For dessert, well, I wont spoil the surprise, but prepare to gaze intothe future of JavaScript! This book covers features that give you more and more controlover concurrency and asynchronicity.
Well, I wont block your enjoyment of the book any longeron with theshow! If youve already read part of the book before reading thisforeword, give yourself 10 asynchronous points! You deserve them!
Jake Archibald (http://jakearchibald.com, @jaffathecake), Developer Advocate at Google Chrome
Preface
Im sure you noticed, but JS in the series title is not an abbreviation for words used to curse about JavaScript, though cursing at the languages quirks is something we can probably all identify with!
From the earliest days of the Web, JavaScript has been a foundational technology that drives interactive experience around the content we consume. While flickering mouse trails and annoying pop-up prompts may be where JavaScript started, nearly two decades later, the technology and capability of JavaScript has grown many orders of magnitude, and few doubt its importance at the heart of the worlds most widely available software platform: the Web.
But as a language, it has perpetually been a target for a great deal of criticism, owing partly to its heritage but even more to its design philosophy. Even the name evokes, as Brendan Eich once put it, dumb kid brother status next to its more mature older brother Java. But the name is merely an accident of politics and marketing. The two languages are vastly different in many important ways. JavaScript is as related to Java as Carnival is to Car.
Because JavaScript borrows concepts and syntax idioms from several languages, including proud C-style procedural roots as well as subtle, less obvious Scheme/Lisp-style functional roots, it is exceedingly approachable to a broad audience of developers, even those with little to no programming experience. The Hello World of JavaScript is so simple that the language is inviting and easy to get comfortable with in early exposure.
While JavaScript is perhaps one of the easiest languages to get up and running with, its eccentricities make solid mastery of the language a vastly less common occurrence than in many other languages. Where it takes a pretty in-depth knowledge of a language like C or C++ to write a full-scale program, full-scale production JavaScript can, and often does, barely scratch the surface of what the language can do.
Sophisticated concepts that are deeply rooted into the language tend instead to surface themselves in seemingly simplistic ways, such as passing around functions as callbacks, which encourages the JavaScript developer to just use the language as-is and not worry too much about whats going on under the hood.
It is simultaneously a simple, easy-to-use language that has broad appeal, and a complex and nuanced collection of language mechanics that without careful study will elude true understanding even for the most seasoned of JavaScript developers.
Therein lies the paradox of JavaScript, the Achilles heel of the language, the challenge we are presently addressing. Because JavaScript can be used without understanding, the understanding of the language is often never attained.
Mission
If at every point that you encounter a surprise or frustration in JavaScript, your response is to add it to the blacklist (as some are accustomed to doing), you soon will be relegated to a hollow shell of the richness of JavaScript.
While this subset has been famously dubbed The Good Parts, I would implore you, dear reader, to instead consider it the The Easy Parts, The Safe Parts, or even The Incomplete Parts.
This You Dont Know JS series offers a contrary challenge: learn and deeply understand all of JavaScript, even and especially The Tough Parts.