frAgile Developer

Stop asking me to do bad things.

26 May 2021

DTTAH: Infinite, Asynchronous Recursion

(Don’t Try This At Home)

I am approximately a thousand percent sure this has been successfully implemented elsewhere with complete success, unless the idea is just completely stupid (which is possible), but I had a random question pop into my head the other day: Does recursion really have to be limited?

Well, this proof of concept is slow, but my curiosity has led me down this line of thinking. Using JavaScript as my POC language, we need a helper function.

async function recurse(f, ...args) {
  return new Promise((resolve, reject) => {
    setImmediate(() => {
      try {
        resolve(f(...args));
      } catch (err) {
        reject(err);
      }
    });
  });
};

Using this, we can write a simple function that recurses without ever blowing the stack, and which could conceivably be distributed, paused, stored, restored, etc.:

async function recursiveCount(target, current = 0) {
  if (current >= target) return current;
  if (current % 1000 === 0) console.log(`${current} ...`);
  return await recurse(recursiveCount, target, current + 1);
};

And run it:

console.log('running ...');
console.log(await recursiveCount(10_000));
console.log('done.);

The console will log:

running ...
0 ...
1000 ....
2000 ....
3000 ....
4000 ....
5000 ....
6000 ....
7000 ....
8000 ....
9000 ....
10000 ....
done.

It won’t be spectacularly fast. I’m sure there are other ways to manage the call stack, especially in other languages. But, if you really really want to write a function recursively that has a real possibility of blowing the stack, DON’T. (At least not not “professionally” … :D … )

In all seriousness though, having done zero actual research, has anyone does something like this before? Please let me know what you’ve seen and tried in the comments below!

Back to index ...