Nicholas C. Zakas
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
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 doesnt 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, its 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 books 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 youll 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
Im grateful to my father, Speros Zakas, for copyediting this book and for Rob Friesels technical editing. You both have made this book much better than it was.
Thanks to everyone who reviewed early versions of this book and provided feedback: Mike Sherov, David Hund, Murat Corlu, and Chris Ferdinandi.
About the Author
Nicholas C. Zakas is an independent software engineer, consultant, and coach. He is the creator of the ESLint open source project and serves on the ESLint Technical Steering Committee. Nicholas works with companies and individuals to improve software engineering processes and helps technical leaders grow and succeed. He has also authored or contributed to over a dozen books related to JavaScript and web development. You can find Nicholas online at https://humanwhocodes.com and on Twitter @slicknet.
Disclaimer
While the publisher and the author have used good faith effort 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.
1. Promise Basics
While promises are often associated with asynchronous operations, they are simply placeholders for values. The value may already be known or, more commonly, the value may be the result of an asynchronous operation. Instead of subscribing to an event or passing a callback to a function, a function can return a promise, like this: