The Problem with Infinite Recursive Promise Resolution Chains

Learn how infinite chains of unresolved promises can lead to memory leaks.

At this point in the chapter, we should have a strong understanding of how promises work and how to use them to implement the most common control flow constructs. This is therefore the right time to discuss an advanced topic that every professional Node.js developer should know and understand. This advanced topic is about a memory leak caused by infinite Promise resolution chains. The bug seems to affect the actual Promises/A+ specification, so no compliant implementation is immune.

It’s really common in programming to have tasks that don’t have a predefined ending or take a potentially infinite array of data as input. We can include things like the encoding/decoding of live audio/video streams, the processing of live cryptocurrency market data, and the monitoring of IoTInternet of Things sensors in this category. But we can have much more trivial situations than those, for example, when making heavy use of functional programming.

Infinite operation using promises

To take a simple example, let’s consider the following code, which defines a simple infinite operation using promises:

Get hands-on with 1400+ tech skills courses.