Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Professional JavaScript for Web Developers

You're reading from   Professional JavaScript for Web Developers Discover an easy-to-learn guide to upgrade your JavaScript skills

Arrow left icon
Product type Paperback
Published in Nov 2019
Publisher Wiley
ISBN-13 9781119366447
Length 1144 pages
Edition 4th Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Matt Frisbie Matt Frisbie
Author Profile Icon Matt Frisbie
Matt Frisbie
Arrow right icon
View More author details
Toc

Table of Contents (37) Chapters Close

COVER FREE CHAPTER
FOREWORD
INTRODUCTION 1 What Is JavaScript? 2 JavaScript in HTML 3 Language Basics 4 Variables, Scope, and Memory 5 Basic Reference Types 6 Collection Reference Types 7 Iterators and Generators 8 Objects, Classes, and Object-Oriented Programming 9 Proxies and Reflect 10 Functions 11 Promises and Async Functions 12 The Browser Object Model 13 Client Detection 14 The Document Object Model 15 DOM Extensions 16 DOM Levels 2 and 3 17 Events 18 Animation and Graphics with Canvas 19 Scripting Forms 20 JavaScript APIs 21 Error Handling and Debugging 22 XML in JavaScript 23 JSON 24 Network Requests and Remote Resources 25 Client-Side Storage 26 Modules 27 Workers 28 Best Practices A ES2018 and ES2019 B Strict Mode C JavaScript Libraries and Frameworks D JavaScript Tools INDEX
END USER LICENSE AGREEMENT

PROMISE FINALLY() DEFINITION

Formerly, there were only inelegant ways of defining behavior that would execute after a promise exits the “pending” state no matter the outcome. Usually, this would take the form of recycling the handler:

let resolveA, rejectB;

function finalHandler() {
 console.log('finished');
}

function resolveHandler(val) {
 console.log('resolved');
 finalHandler();
}

function rejectHandler(err) {
 console.log('rejected');
 finalHandler();
}

new Promise((resolve, reject) => {
 resolveA = resolve;
})
.then(resolveHandler, rejectHandler);

new Promise((resolve, reject) => {
 rejectB = reject;
})
.then(resolveHandler, rejectHandler);
 
resolveA();
rejectB();
// resolved
// finished
// rejected
// finished

With Promise.prototype.finally(), you are able to unify the shared handler. The finally() handler is not passed any arguments and does not know if it is handling a resolved or rejected promise. The preceding example...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime