Take the 2-minute tour ×

as I use Node.js for not too long time yet, I've ran into a following problem. I understand that using callback-driven paradigma we need to convert loops we used in synchronous code into recursion. My problem is I cannot understand how deep node can go with recursion, results with different tests are inconsistent. For example, I tried code I've found on Web:

    var depth = 0;

    (function recurseBaby() {
        // log at every 500 calls
        (++depth % 500) || console.log(depth);

        // bail out ~100K depth in case you're special and don't error out
        if (depth > 100000) return;


It gives me node exception (max recursion depth) after 18500 recursions. So I tried to add some functionality, like working with queue:

var depth = 0,
    Memcached = require('memcached'),
    memcacheq = new Memcached('');

(function recurseBaby() {
    // log at every 500 calls

    // bail out ~10M depth in case you're special and don't error out
    if (depth > 10000000) return;

    memcacheq.set('test_queue', 'recurs' + depth, 0, function (error, response) {
        return recurseBaby();

It didn't finish yet, but works so far for more than 4 million recursions (and queue is actually getting filled). So I would like to clarify, how recursion depth limit works in node. My guess would be that if I do something in that recursively called function, node has more time to free call stack, but I may be terribly wrong. Any clarifications from more experienced node users are welcome.

share|improve this question

1 Answer 1

up vote 5 down vote accepted

There is no recursion in your second example at all.

In the first example you clearly have a recursive call.

In the second example a call is made to memcacheq.set which returns immediately, and then your function returns. It will have made a node somewhere of the function to call when the triggered event happens at some later time.

--- No recursive call has been made ---

At some point in time later the memcache function will finish and queue an event to the event queue to trigger your callback.

But note that this is done from an event in the event queue, NOT by a direct recursive call from your function which has probably long since returned.

share|improve this answer
So, if I've understood your answer correctly, as long as I do not use recursive calls directly, but call it from inside of some callback of asynchronous function like db query or so, I shall not run into recursion stack overflow problem? A task at hand now is to do chain of pretty lengthy actions for every record in database collection and proceed to next record only after previous is processed fully, so in actual code I'm using an approach similar to 2nd example in original question, just more (4-5) callbacks in chain. –  Ilya Konstantinov Oct 4 '11 at 9:44
As long as the function returns and does the callback by a queue later on then yes no problem. However if the callback is done from within the function you called then there is a problem, and it can be hard to tell which the called function does., –  jcoder Oct 4 '11 at 9:48

Your Answer


