Just to correct what @gabriel.hayes said:
.then()
is not recursive. Each step is independent of the next, with only the resolved value being passed along, thus once a .then()
step completes, its memory can usually be released unless it holds references.
In contrast, recursion involves a function calling itself (or another function in a recursive chain). Each step in a recursive process remains unresolved until all subsequent steps resolve. For example, if Func(n)
calls Func(n+1)
, it cannot finish until Func(n+1)
resolves, which in turn depends on Func(n+2)
, and so on. This creates a stack of unresolved calls that stays in memory until the recursion fully unwinds.
You can think of .then()
as passing a message along a chain: once the message is passed, you're done. In recursion, each step requests something from the next and must wait for a response, leaving it unresolved until the response arrives.
An analogy for this could be a restaurant:
.then()
: You tell the waiter to give the chef your best regards,So no, every part of the .then()
chain is not retained in memory until the final step resolves.
If you want a more in-depth look at how it works under the hood, this video does a great job of visualizing it.
And as we just entered 2025, I'm wishing you all a Happy New Year! 🎉