Node-Fibers A1.6 | The Future of JavaScript: Generators Copyright 2012, The Pragmatic Bookshelf. |
Early Praise for Async JavaScript Async JavaScript is the first full book Ive seen dedicated to a key topic in JavaScript development today: how to deal with concurrency and concurrent tasks without going crazy! For the sake of your sanity, check this out. Peter Cooper, editor of JavaScript Weekly |
Trevor delivers a concise guide to writing asynchronous JavaScript with a perfect balance of browser and server-side examples. Part guide, part overview, wholly engaging, this book is a must-read for any JavaScript developer looking to level up. Wynn Netherland, co-host of The Changelog |
This is a complete guide to the asynchronous realm of JavaScript. The concepts and tools covered by this book are essential to anyone willing to build full-blown, well-structured and efficient JavaScript applications. Julien Biezemans, Ruby/JavaScript developer, author of Cucumber.js |
Acknowledgments
It was not love at first sight with me and JavaScript. Yet today, its one of my two favorite programming languages. The other? Its little brother, CoffeeScript. The story of how I learned to stop worrying and love JavaScripting is a story shared by tens of thousands of programmers. Id like to thank those who took JavaScript seriously from the start, shaping the rich development ecosystem the language enjoys today: John Resig, for creating the browsers de facto standard library, jQuery; Jeremy Ashkenas, for producing CoffeeScript and the rich yet minimalistic Backbone.js framework; Ryan Dahl, for giving the language a robust server environment; and all the other programmers whove proven through their work that JavaScript is a first-class language after all. Of course, love alone didnt write this book. Id like to thank the Pragmatic Bookshelf team for helping me thoroughly renovate my original KickStarted manuscript and raise it to the standard of quality that PragProg is famous for. Particular thanks go to managing editor Susannah Pfalzer, head honchos Dave Thomas and Andy Hunt, and most of all my editor, Jackie Carter. Their savvy and motivation have been invaluable. Thanks also to my technical reviewers for this edition: Julien Biezemans, Christophe Porteneuve, Michael Ficarra, Travis Swicegood, and Lon Ingram. Special thanks to Karl Stolley for going above and beyond in multiple reviews. Id also like to thank Stan Angeloff and Roly Fentanes for reviewing the original manuscript. Any remaining errors are entirely my fault. Thanks, finally, to my employer, HubSpot, for supporting me as I brought this book to completion. After years of nomadic freelancing, Ive finally found a home. Trevor Burnham November 2012 Copyright 2012, The Pragmatic Bookshelf. Introduction
Originally devised to enhance web pages in Netscape 2.0, JavaScript is now faced with being a single-threaded language in a multimedia, multitasking, multicore world. Yet JavaScript has not only persevered since 1995, its thrived. One after the other, potential rivals in the browserFlash, Silverlight, and Java applets, to name a fewhave come and (more or less) gone. Meanwhile, when a programmer named Ryan Dahl wanted to build a new framework for event-driven servers, he searched the far reaches of computer science for a language that was both dynamic and single-threaded before realizing that the answer was right in front of him. And so, Node.js was born, and JavaScript became a force to be reckoned with in the server world. How did this happen? As recently as 2001, Paul Graham wrote the following in his essay The Other Road Ahead: I would not even use JavaScript, if I were you Most of the JavaScript I see on the Web isnt necessary, and much of it breaks. Today, Graham is the lead partner at Y Combinator, the investment group behind Dropbox, Heroku, and hundreds of other start-upsnearly all of which use JavaScript. As he put it in a revised version of the essay, JavaScript now works. When did JavaScript become a respectable language? Some say the turning point was Gmail (2004), which showed the world that with a heavy dose of Ajax you could run a first-class email client in the browser. Others say that it was jQuery (2006), which abstracted the rival browser APIs of the time to create a de facto standard. (As of 2011, 48 percent of the top 17,000 websites use jQuery.) Whatever the reason, JavaScript is here to stay. Apple got behind JavaScript with WebKit and Safari. Microsoft is getting behind JavaScript with Metro. Even Adobe is getting behind JavaScript with tools to generate HTML5 instead of Flash. What began as a humble browser feature has become arguably the most important programming language in the world. Thanks to the ubiquity of web browsers, JavaScript has come closer than any other language to fulfilling Javas old promise of write once, run anywhere. In 2007, Jeff Atwood coined Atwoods law: |