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
The Principles of Object-Oriented JavaScript Rating: 4 out of 5 stars4/5Understanding ECMAScript 6: The Definitive Guide for JavaScript Developers Rating: 4 out of 5 stars4/5Professional JavaScript for Web Developers Rating: 0 out of 5 stars0 ratings
Related to Understanding JavaScript Promises
Related ebooks
Decoding JavaScript: A Simple Guide for the Not-so-Simple JavaScript Concepts, Libraries, Tools, and Frameworks (English Edition) Rating: 0 out of 5 stars0 ratingsNode.js Web Development - Third Edition Rating: 2 out of 5 stars2/5Learn NodeJS in 1 Day: Complete Node JS Guide with Examples Rating: 3 out of 5 stars3/5Mastering Node.js Rating: 1 out of 5 stars1/5Learn React Hooks: Build and refactor modern React.js applications using Hooks Rating: 0 out of 5 stars0 ratingsNode.js, JavaScript, API: Interview Questions and Answers Rating: 5 out of 5 stars5/5TypeScript Design Patterns Rating: 0 out of 5 stars0 ratingsNode.js 63 Interview Questions and Answers Rating: 0 out of 5 stars0 ratingsYour First Week With Node.js Rating: 0 out of 5 stars0 ratingsMastering Reactive JavaScript Rating: 0 out of 5 stars0 ratingsObject-Oriented JavaScript: Create scalable, reusable high-quality JavaScript applications, and libraries Rating: 3 out of 5 stars3/5The Joy of JavaScript Rating: 4 out of 5 stars4/5Node.JS Guidebook: Comprehensive guide to learn Node.js Rating: 0 out of 5 stars0 ratingsMastering JavaScript: The Complete Guide to JavaScript Mastery Rating: 5 out of 5 stars5/5Node.js: Novice to Ninja Rating: 0 out of 5 stars0 ratingsAdvanced Web Development with React: SSR and PWA with Next.js using React with advanced concepts Rating: 0 out of 5 stars0 ratingsModern JavaScript Applications Rating: 0 out of 5 stars0 ratings50 Recipes for Programming Node.js Rating: 3 out of 5 stars3/5JavaScript: Tips and Tricks to Programming Code with Javascript: JavaScript Computer Programming, #2 Rating: 0 out of 5 stars0 ratingsNode.js By Example Rating: 2 out of 5 stars2/5React Deep Dive Rating: 5 out of 5 stars5/550 Recipes for Programming CSS3 Rating: 0 out of 5 stars0 ratingsClean Code in JavaScript: Develop reliable, maintainable, and robust JavaScript Rating: 4 out of 5 stars4/5JavaScript Patterns JumpStart Guide (Clean up your JavaScript Code) Rating: 4 out of 5 stars4/5React Projects: Build 12 real-world applications from scratch using React, React Native, and React 360 Rating: 0 out of 5 stars0 ratingsJavaScript: Best Practices to Programming Code with JavaScript: JavaScript Computer Programming, #3 Rating: 0 out of 5 stars0 ratingsObject-Oriented JavaScript Rating: 4 out of 5 stars4/5
Internet & Web For You
Cybersecurity For Dummies Rating: 5 out of 5 stars5/5Coding For Dummies 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: 4 out of 5 stars4/5How to Be Invisible: Protect Your Home, Your Children, Your Assets, and Your Life Rating: 4 out of 5 stars4/5Social Engineering: The Science of Human Hacking Rating: 3 out of 5 stars3/548 Really Useful Web Sites Rating: 5 out of 5 stars5/5The Beginner's Affiliate Marketing Blueprint Rating: 4 out of 5 stars4/5Mastering YouTube Automation: The Ultimate Guide to Creating a Successful Faceless Channel Rating: 0 out of 5 stars0 ratingsPython: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Kill All Normies: Online Culture Wars From 4Chan And Tumblr To Trump And The Alt-Right Rating: 3 out of 5 stars3/5SEO For Dummies Rating: 4 out of 5 stars4/5How To Start A Profitable Authority Blog In Under One Hour Rating: 5 out of 5 stars5/5Notion for Beginners: Notion for Work, Play, and Productivity Rating: 4 out of 5 stars4/5WordPress For Dummies Rating: 0 out of 5 stars0 ratingsThe Digital Marketing Handbook: A Step-By-Step Guide to Creating Websites That Sell Rating: 5 out of 5 stars5/5Build a WordPress Website From Scratch 2024: WordPress 2024 Rating: 0 out of 5 stars0 ratingsAn Ultimate Guide to Kali Linux for Beginners Rating: 3 out of 5 stars3/5How to Disappear and Live Off the Grid: A CIA Insider's Guide Rating: 0 out of 5 stars0 ratingsTor and the Dark Art of Anonymity Rating: 5 out of 5 stars5/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5Content Chemistry: The Illustrated Handbook for Content Marketing Rating: 5 out of 5 stars5/5Surveillance and Surveillance Detection: A CIA Insider's Guide Rating: 3 out of 5 stars3/5Blogging For Dummies Rating: 0 out of 5 stars0 ratingsNo Place to Hide: Edward Snowden, the NSA, and the U.S. Surveillance State Rating: 4 out of 5 stars4/5Ready, Set, Brand!: The Canva for Work Quickstart Guide Rating: 5 out of 5 stars5/5
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