The Asynchronous Programming Model
Total Page:16
File Type:pdf, Size:1020Kb
www.allitebooks.com Mastering JavaScript Promises Discover and explore the world of promises, one of JavaScript's most powerful concepts Muzzamil Hussain BIRMINGHAM - MUMBAI www.allitebooks.com Mastering JavaScript Promises Copyright © 2015 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: July 2015 Production reference: 1210715 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78398-550-0 www.packtpub.com www.allitebooks.com Credits Author Project Coordinator Muzzamil Hussain Harshal Ved Reviewers Proofreader Luca Mezzalira Safis Editing Jebin B V Indexer Commissioning Editor Priya Sane Edward Gordon Graphics Acquisition Editor Sheetal Aute Meeta Rajani Production Coordinator Content Development Editor Komal Ramchandani Ajinkya Paranjape Cover Work Technical Editor Komal Ramchandani Siddhi Rane Copy Editor Janbal Dharmaraj www.allitebooks.com About the Author Muzzamil Hussain has been working in the field of computing/information technology for more than a decade now. During this period, he developed a wide range of software products and services for companies around the world. He completed his Digital Communications Networks course from the prestigious London Metropolitan University, London, United Kingdom. He is skilled in crafting software products and services with the best of technical and management skills, which he has acquired over the past 14 years. The best part of his wealth of experience is the dual skill sets of project management and software engineering, which is a paramount feature of his work. Muzzamil has worked with some of the big industries based in Pakistan, the UK, the USA, Canada, and UAE. These days, he is associated with Systems Limited, the first and one of the oldest software companies in Pakistan, in the capacity of senior project manager. He also provides consultancy to start-ups on the software development life cycle, project management, release engineering, and continuous integrations. He is also heavily involved in experimenting with the latest technologies such as the MEAN stack, an opinionated full-stack JavaScript framework, and Apache Hadoop for the distributed processing of large data sets. www.allitebooks.com He blogs at http://muzzamil.net/. There are several people I would like to acknowledge and thank everyone who helped me not only in writing this book, but also those who were an inspiration to my professional growth, making me a better person. Thanks to all those amazing people around me: my father, Mr. Musharraf Hussain, and my mother, Mrs. Rifat Hussain, who raised me with their best resources and provided a healthy foundation for my life. My teachers, who not only educated me with their best skills and knowledge, but also made me understand the tough lessons of life. Finally, my wife, Amber Muzzamil, and my daughters, Abeeha and Aroush, whose continuous and unconditional support was the biggest factor in making this book a reality. www.allitebooks.com About the Reviewers Luca Mezzalira is a passionate Italian software developer with more than 10 years of experience in frontend technologies, in particular, JavaScript, HTML 5, Haxe, Flash, Flex, AIR, Lua, and Swift. He has often been involved in cutting-edge projects for mobile (iOS, Android, and Blackberry), desktop, web, and embedded devices too for big corporations. He really loves his job and tries to apply the Kaizen culture of continuous improvement and the XP principles and values in his daily life. He strongly believes that agile and lean methodologies can help you achieve any goal during your job, improving yourself and the people you are working with. In his spare time, Luca learns new technologies and methodologies by reading books and attending meetup events or conferences. He is very flexible and adaptive to any situation, always trying to achieve great goals in the best way possible. He has collected different certifications and acknowledgements across the last 10 years, such as Certified Scrum Master and SAFe Agilist; Adobe Certified Expert and instructor on Flash, Flex, AIR, and Flash Lite; Adobe Community Professional; and Adobe Italy Consultant. He has written for national and international technical magazines and is a technical reviewer for Packt Publishing. He speaks at national and international conferences and community events, such as Lean Kanban United Kingdom, Flash Camp, Scotch on the Rocks, 360 Flex, PyCon, and so on. www.allitebooks.com In his spare time, Luca likes to watch football, play with his dogs, Paco and Maya, and study new programming languages. The first mention is for my family that always helps me, in particular, my parents who support and inspire me everyday with their strength and love. A big thanks to my brother, who is also one of my best friends. He is the most intelligent person that I've ever met in my life; his suggestions and ideas are very important to me. Then, I really have a lot of other friends to say thanks to for what we have created together until now. I hope to not forget anybody: Piergiorgio Niero, Chiara Agazzi, Alessandro Bianco, Raffaella Brandoli, Miguel Barreiro, Mark Stanley, Frank Amankwah, Matteo Oriani, Manuele Mimo, Goy Oracha, Tommaso Magro, Sofia Faggian, Matteo Lanzi, Peter Elst, Francesca Beordo, Federico Pitone, Tiziano Fruet, Giorgio Pedergnani, Andrea Sgaravato, Fabio Bernardi, Sumi Lim, and many others. Last but not least, I'd like to say thanks to my girlfriend and my life partner, Maela, for the amazing time we spend together; her passion and commitment in our relationship gives me the strength to go ahead and do my best everyday. Really, thanks, my love! Jebin B V is a young frontend developer by profession and a full-stack developer. He has been into web development for the past 4 years and has a very good command over the design and development of commercial web applications. He also has a very good sense of design, interaction, and UX when it comes to web development. Jebin has developed applications for real-time messaging, big data management, visualization, network shopping management, CMS, social networking, and so on. He has great interest in JavaScript, so anything that is from the JavaScript background excites him. He also has experience in PHP and Java. He possesses a very good notion of application-level design when it comes to building frontend applications. He has the nonstop habit of learning on an everyday basis. He spends a great deal of time on updating himself with new things coming up in frontend technologies. He loves to learn, teach, master, and lead in his field of expertise. www.allitebooks.com www.PacktPub.com Support files, eBooks, discount offers, and more For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub. com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. TM https://www2.packtpub.com/books/subscription/packtlib Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books. Why subscribe? • Fully searchable across every book published by Packt • Copy and paste, print, and bookmark content • On demand and accessible via a web browser Free access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access. www.allitebooks.com Table of Contents Preface vii Chapter 1: Promises.js 1 The fall and rise of JavaScript 2 Google's contributions to JavaScript 2 Where Promises.js came in? 2 What is a promise? 3 Why do we need promise in JS? 3 Software prerequisites 4 Environment you need before getting started 4 Future, promise, and delay 4 Promise pipelining 5 Read-only views 5 States of a promise 6 How do we keep Promises.js in this book? 6 Browser compatibility 7 Summary 7 Chapter 2: The JavaScript Asynchronous Model 9 Programming models 9 The single-threaded synchronous model 10 The multithreaded synchronous model 10 The asynchronous programming model 11 Densities with an asynchronous programming model 12 Why do we need to block the task? 12 Why not use some more threads? 13 Learning