Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Hands-On Functional Programming with TypeScript

You're reading from   Hands-On Functional Programming with TypeScript Explore functional and reactive programming to create robust and testable TypeScript applications

Arrow left icon
Product type Paperback
Published in Jan 2019
Publisher Packt
ISBN-13 9781788831437
Length 210 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
 Jansen Jansen
Author Profile Icon Jansen
Jansen
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Functional Programming Fundamentals FREE CHAPTER 2. Mastering Functions 3. Mastering Asynchronous Programming 4. The Runtime – The Event Loop and the this Operator 5. The Runtime – Closures and Prototypes 6. Functional Programming Techniques 7. Category Theory 8. Immutability, Optics, and Laziness 9. Functional-Reactive Programming 10. Real-World Functional Programming 11. Functional Programming Learning Road Map 12. Directory of TypeScript Functional Programming Libraries 13. Other Books You May Enjoy

Callback hell

We have learned that callbacks and higher-order functions are two powerful and flexible JavaScript and TypeScript features. However, the use of callbacks can lead to a maintainability issue known as callback hell.

We are now going to write an example to showcase callback hell. We are going to write three functions with the same behavior.

The first function is named doSomethingAsync. The function takes an array of numbers as one of its arguments and adds a new number to it. The function uses setTimeout to simulate some I/O operation, such as reading from a database, and Math.ramdom to simulate a potential exception, such as a request timeout:

function doSomethingAsync(
arr: number[],
success: (arr: number[]) => void,
error: (e: Error) => void
) {
setTimeout(() => {
try {
let n = Math.ceil(Math.random() * 100 + 1);
if...
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