Continuation passing style

If we have recursive calls stacked too high, we already know that our logic will fail. On the other hand, we know that tail calls should alleviate that problem, but don't, because of browser implementations! However, there's a way out of this. Let's first consider how we can transform recursive calls into tail calls by using a well-known FP concept—continuations—and we'll leave the problem of solving TCO limitations for the next section. (We mentioned continuations in the Callbacks, promises, and continuations section of Chapter 3, Starting Out with Functions – A Core Concept, but we didn't go into detail.)

In FP parlance, a continuation is something that represents the state of a process and allows processing to ...

Get Mastering JavaScript Functional Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.