Understanding JavaScript Promises
()
About this ebook
Nicholas C. Zakas
Nicholas C. Zakas is a front-end consultant, author, and speaker. He worked at Yahoo! for almost five years, where he was front-end tech lead for the Yahoo! homepage and a contributor to the YUI library. He is the author of Professional JavaScript for Web Developers (Wrox, 2012), Professional Ajax (Wrox, 2007), and High Performance JavaScript(O’Reilly, 2010). Nicholas is a strong advocate for development best practices including progressive enhancement, accessibility, performance, scalability, and maintainability. He blogs regularly at http://www.nczonline.net/ and can be found on Twitter via @slicknet.
Read more from Nicholas C. Zakas
Professional Ajax Rating: 3 out of 5 stars3/5Professional JavaScript for Web Developers Rating: 0 out of 5 stars0 ratings
Related to Understanding JavaScript Promises
Related ebooks
The Joy of JavaScript Rating: 0 out of 5 stars0 ratingsJavaScript: Tips and Tricks to Programming Code with Javascript: JavaScript Computer Programming, #2 Rating: 0 out of 5 stars0 ratingsNode.js: Novice to Ninja Rating: 0 out of 5 stars0 ratingsJavaScript Enlightenment Rating: 4 out of 5 stars4/5JavaScript Patterns JumpStart Guide (Clean up your JavaScript Code) Rating: 4 out of 5 stars4/5React Deep Dive Rating: 5 out of 5 stars5/5Advanced Web Development with React: SSR and PWA with Next.js using React with advanced concepts Rating: 0 out of 5 stars0 ratingsJavaScript: Beginner's Guide to Programming Code with JavaScript: JavaScript Computer Programming Rating: 0 out of 5 stars0 ratingsJavaScript: Tips and Tricks to Programming Code with Javascript Rating: 0 out of 5 stars0 ratingsJavaScript for Gurus: Use JavaScript programming features, techniques and modules to solve everyday problems Rating: 0 out of 5 stars0 ratingsMastering JavaScript Rating: 4 out of 5 stars4/5React Components Rating: 0 out of 5 stars0 ratingsComputer Programming: From Beginner to Badass—JavaScript, HTML, CSS, & SQL Rating: 3 out of 5 stars3/5React.js Design Patterns: Learn how to build scalable React apps with ease (English Edition) Rating: 0 out of 5 stars0 ratingsArt of Clean Code: How to Write Codes for Human Rating: 3 out of 5 stars3/5JavaScript: Beginner's Guide to Programming Code with JavaScript Rating: 5 out of 5 stars5/5Decoding JavaScript: A Simple Guide for the Not-so-Simple JavaScript Concepts, Libraries, Tools, and Frameworks (English Edition) Rating: 0 out of 5 stars0 ratingsMastering JavaScript Object-Oriented Programming Rating: 0 out of 5 stars0 ratingsBuilding large scale web apps Rating: 0 out of 5 stars0 ratingsMastering JavaScript Promises Rating: 1 out of 5 stars1/5Jump Start Vue.js Rating: 4 out of 5 stars4/5ReactJS by Example - Building Modern Web Applications with React Rating: 4 out of 5 stars4/5Javascript Concepts: 1St Edition Rating: 0 out of 5 stars0 ratingsReact Design Patterns and Best Practices Rating: 0 out of 5 stars0 ratingsJava: Tips and Tricks to Programming Code with Java Rating: 0 out of 5 stars0 ratingsSecrets of the JavaScript Ninja Rating: 4 out of 5 stars4/550 Recipes for Programming Node.js Rating: 3 out of 5 stars3/5JavaScript: The New Toys Rating: 0 out of 5 stars0 ratings
Internet & Web For You
Beginner's Guide To Starting An Etsy Print-On-Demand Shop Rating: 0 out of 5 stars0 ratingsGrokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5The Digital Marketing Handbook: A Step-By-Step Guide to Creating Websites That Sell Rating: 5 out of 5 stars5/5More Porn - Faster!: 50 Tips & Tools for Faster and More Efficient Porn Browsing Rating: 3 out of 5 stars3/5The $1,000,000 Web Designer Guide: A Practical Guide for Wealth and Freedom as an Online Freelancer Rating: 5 out of 5 stars5/5The Logo Brainstorm Book: A Comprehensive Guide for Exploring Design Directions Rating: 4 out of 5 stars4/5Cybersecurity For Dummies Rating: 4 out of 5 stars4/5From Nothing Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5SEO For Dummies Rating: 4 out of 5 stars4/5Six Figure Blogging In 3 Months Rating: 4 out of 5 stars4/5Coding For Dummies Rating: 5 out of 5 stars5/5Six Figure Blogging Blueprint Rating: 5 out of 5 stars5/5Hacking : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Ethical Hacking Rating: 5 out of 5 stars5/5C++ Learn in 24 Hours Rating: 0 out of 5 stars0 ratingsThe Beginner's Affiliate Marketing Blueprint Rating: 4 out of 5 stars4/5Wireless Hacking 101 Rating: 4 out of 5 stars4/5The Mega Box: The Ultimate Guide to the Best Free Resources on the Internet Rating: 4 out of 5 stars4/5Remote/WebCam Notarization : Basic Understanding Rating: 3 out of 5 stars3/5Get Rich or Lie Trying: Ambition and Deceit in the New Influencer Economy Rating: 0 out of 5 stars0 ratingsMike Meyers' CompTIA Security+ Certification Guide, Third Edition (Exam SY0-601) Rating: 5 out of 5 stars5/5Wordpress for Beginners: The Easy Step-by-Step Guide to Creating a Website with WordPress Rating: 5 out of 5 stars5/5How To Start A Podcast Rating: 4 out of 5 stars4/5Tor and the Dark Art of Anonymity Rating: 5 out of 5 stars5/5Stop Asking Questions: How to Lead High-Impact Interviews and Learn Anything from Anyone Rating: 5 out of 5 stars5/5Podcasting For Dummies Rating: 4 out of 5 stars4/5How To Make Money Blogging: How I Replaced My Day-Job With My Blog and How You Can Start A Blog Today Rating: 4 out of 5 stars4/5The Designer's Web Handbook: What You Need to Know to Create for the Web Rating: 0 out of 5 stars0 ratingsHow to Disappear and Live Off the Grid: A CIA Insider's Guide Rating: 0 out of 5 stars0 ratings
Reviews for Understanding JavaScript Promises
0 ratings0 reviews
Book preview
Understanding JavaScript Promises - Nicholas C. Zakas
Understanding JavaScript Promises
Nicholas C. Zakas
This book is for sale at http://leanpub.com/understanding-javascript-promises
This version was published on 2022-02-17
publisher's logo* * * * *
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do.
* * * * *
© 2020 - 2022 Nicholas C. Zakas
ISBN for EPUB version: 978-1-6781-2066-5
Table of Contents
Introduction
About This Book
Acknowledgments
About the Author
Disclaimer
1. Promise Basics
The Promise Lifecycle
Creating New (Unsettled) Promises
Creating Settled Promises
Summary
2. Chaining Promises
Catching Errors
Using finally() in Promise Chains
Returning Values in Promise Chains
Returning Promises in Promise Chains
Summary
3. Working with Multiple Promises
The Promise.all() Method
The Promise.allSettled() Method
The Promise.any() Method
The Promise.race() Method
Summary
4. Async Functions and Await Expressions
Defining Async Functions
What Makes Async Functions Different
Summary
5. Unhandled Rejection Tracking
Detecting Unhandled Rejections
Web Browser Unhandled Rejection Tracking
Node.js Unhandled Rejection Tracking
Summary
Final Thoughts
Download the Extras
Support the Author
Help and Support
Follow the Author
Notes
Introduction
One of the most powerful aspects of JavaScript is how easily it handles asynchronous programming. As a language created for the web, JavaScript needed to respond to user interactions such as clicks and key presses from the beginning, and so event handlers such as onclick were created. Event handlers allowed developers to specify a function to execute at some later point in time in reaction to an event.
Node.js further popularized asynchronous programming in JavaScript by using callback functions in addition to events. As more and more programs started using asynchronous programming, events and callbacks were no longer sufficient to support everything developers wanted to do. Promises are the solution to this problem.
Promises are another option for asynchronous programming, and they work like futures and deferreds do in other languages. A promise specifies some code to be executed later (as with events and callbacks) and also explicitly indicates whether the code succeeded or failed at its job. You can chain promises together based on success or failure in ways that make your code easier to understand and debug.
About This Book
The goal of this book is to explain how JavaScript promises work while giving practical examples of when to use them. All new asynchronous JavaScript APIs will be built with promises going forward, and so promises are a central concept to understanding JavaScript as a whole. My hope is that this book will give you the information you need to successfully use promises in your projects.
Browser, Node.js, and Deno Compatibility
There are multiple JavaScript runtimes that you may use, such as web browsers, Node.js, and Deno. This book doesn’t attempt to address differences between these JavaScript runtimes unless they are so different as to be confusing. In general, this book focuses on promises as described in ECMA-262 and only talks about differences in JavaScript runtimes when they are substantially different. As such, it’s possible that your JavaScript runtime may not conform to the standards-based behavior described in this book.
Who This Book Is for
This book is intended as a guide for those who are already familiar with JavaScript. In particular, this book is aimed at intermediate-to-advanced JavaScript developers who work in web browsers, Node.js, or Deno and who want to learn how promises work.
This book is not for beginners who have never written JavaScript. You will need to have a good, basic understanding of the language to make use of this book.
Overview
Each of this book’s five chapters covers a different aspect of JavaScript promises. Many chapters cover promise APIs directly, and each chapter builds upon the preceding chapters in a way that allows you to build up your knowledge gradually. All chapters include code examples to help you learn new syntax and concepts.
Chapter 1: Promise Basics introduces the concept of promises, how they work, and different ways to create and use them.
Chapter 2: Chaining Promises discusses the various ways to chain multiple promises together to make composing asynchronous operations easier.
Chapter 3: Working with Multiple Promises explains the built-in JavaScript methods designed to monitor and respond to multiple promises executing in parallel.
Chapter 4: Async Functions and Await Expressions introduces the concepts of async functions and await expressions, and explains how they relate to and use promises.
Chapter 5: Unhandled Rejection Tracking explains how to properly track when promises are rejected without a rejection handler.
Conventions Used
The following typographical conventions are used in this book:
Italics introduces new terms
Constant width indicates a piece of code or filename
All JavaScript code examples are written as modules (also known as ECMAScript modules or ESM).
Additionally, longer code examples are contained in constant width code blocks such as:
1 function doSomething() { 2 // empty 3 }
Within a code block, comments to the right of a console.log() statement indicate the output you’ll see in the browser or Node.js console when the code is executed. For example:
1 console.log(Hi
); // Hi
If a line of code in a code block throws an error, this is also indicated to the right of the code:
1 doSomething(); // error!
Help and Support
If you have questions as you read this book, please send a message to my mailing list: books@humanwhocodes.com. Be sure to mention the title of this book in your subject line.
Acknowledgments
I’m grateful to my father, Speros Zakas, for copyediting this book and for Rob Friesel’s technical editing. You both have made this book much better than it