-2

Basically what I'm trying to do boils down to

function a() {
    // Do stuff that waits for things etc
    b(a);
}

function b(f) {
    f()
}

function a() { b(a); }; function b(f) { f(); }; a() That will cause a too much recursion error after a while though. Apparently javascript doesn't support tail-recursion so that won't work either. I can't think of any way to use a loop here either since I don't want the code executed immediately. So any way to do this or is it impossible?

Also I apologize if this has been asked before, couldn't find anything that helped.

Edit: Also before anyone asks, no I'm not actually using setTimeout so setIntervall isn't an option.

Edit again: Alright hope this shows what I'm trying to do better. I need to call the same code after waiting for things to complete again without actually putting it in a loop and blocking the program.

mwerschy
  • 1,698
  • 1
  • 15
  • 26
  • 3
    Unless there something else in "Do stuff", that will never cause a "too much recursion" error because it involves no recursion. – Pointy Oct 25 '14 at 20:43
  • This will not cause a too much recursion error. It has nothing to do with tail recursion. What is the problem you are experiencing with this? Your question is ambiguous. The example shows you using `setTimeout`, yet then you say you are not using it. Are you or aren't you? –  Oct 25 '14 at 20:43
  • 1
    If you're not using `setTimeout`, why did you add this code??? – Rudie Oct 25 '14 at 20:44
  • Just for demonstrating it more easily since the actual code uses a bunch of libs. Let me try to make it more clear. – mwerschy Oct 25 '14 at 20:46
  • 3
    this is not recursion and makes no sense, please clarify your question and/or paste your actual code or an approximation of it. – DataDao Oct 25 '14 at 20:46
  • 1
    Did you test the code you posted for "demonstrating it more easily" and verify that it causes a recursion error, because, uhh, it wouldn't. You said "it **will** cause a recursion error; did you mean to say "it **does** cause", or "I think it **might** cause"? –  Oct 25 '14 at 20:47
  • Alright this better? I tried running this in console and it causes the error. Would setTimeout not do that? – mwerschy Oct 25 '14 at 20:50
  • 1
    If you're going to change your question, then delete this question, and post another one. -1 REMOVE FLAG. –  Oct 25 '14 at 20:52
  • @mwerschy you have **COMPLETELY** changed the nature of the question. What is your **real** code? What problem are you **really** having? – Pointy Oct 25 '14 at 20:52
  • You say you need a function to be invoked "after waiting" and "without blocking", but your new code is definitely blocking – recursive Oct 25 '14 at 20:54
  • Alright sorry guys I'll look at the code again and try to write a better question. Deleting. Edit: Well can't delete I'll just wait for close then. – mwerschy Oct 25 '14 at 20:54
  • Great. So you've written a recursive function, equivalent to `function a() { a(); }`. So what? What is your question? Yes, it's recursive. Yes, it will cause a stack overflow. With tail recursion, instead of stack overflow it will cause an infinite loop. So...what? Is your question how to stop an infinite loop from being an infinite loop? –  Oct 25 '14 at 20:57
  • @torazaburo Sorry oversimplified it and kinda messed it up. Just let it close and I'll ask again later if I can't figure it out. – mwerschy Oct 25 '14 at 20:59
  • "Do stuff that waits for things etc"...do you know what promises are and how to use them? –  Oct 25 '14 at 21:16

1 Answers1

2

Because each call to a() returns before the next invocation, there is no recursion going on. The runtime system repeatedly makes individual calls to the function, and there won't ever be more than one call going on at any given time.

Breaking it down:

  • Somewhere, your code calls a() to start the cycle
  • That call to a() "does stuff" and then invokes setTimeout()
  • The system arranges for the timeout, and that call returns immediately
  • The original call to a() completes
  • 100 milliseconds later, the timer fires and the runtime invokes a()

The cycle just repeats after that.

edit Maybe I should be more explicit: the word recursion refers to a process wherein a function invokes itself (synchronously) either directly or indirectly. Like:

function fibonacci(n) {
  return fibonacci(n - 1) + fibonacci(n - 2);
}

What we have in the code posted in the OP is quite different. There's no call to a() being made from within that function (unless the OP left out some important details). Instead, a reference to the function is being handed over to the system. The call via that reference won't take place until a long, long time after the original call has finished.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • You're not curious where the recursion error comes from and why he would post that irrelevant code?? This is a weird non-answer to a weird non-question. – Rudie Oct 25 '14 at 20:45
  • @Rudie If you know more about what goes on in the "Do stuff" part, you should post that in a comment. Otherwise **there is no recursion**. – Pointy Oct 25 '14 at 20:47
  • Don't tell me. Tell OP. "There is no recursion" isn't an answer to "Why is there a recursion error?". This is why the comments exist. For OP to clarify so he can get an actual answer. – Rudie Oct 25 '14 at 20:48
  • @Rudie there's no way the code in the OP is generating a "too much recursion" error. That's the point. – Pointy Oct 25 '14 at 20:50
  • @dandavis it's simply not an instance of recursion. The call to `setTimeout()` **does not immediately invoke `a()`** so there is no recursion. – Pointy Oct 25 '14 at 20:51
  • Ok I guess `setTimeout` changes what happens then? Edited to try to clarify – mwerschy Oct 25 '14 at 20:53
  • @dandavis it's not really a "feedback" thing at all. The function is simply asking that the system invoke another function after a period of time. It so happens that it's the *same* function, but those two invocations of the function will have no communication or relationship to each other at all. – Pointy Oct 25 '14 at 20:53
  • Interesting question as to whether or not there's a term for this ( I mean, the OP's original question). I'm guessing there is. Meanwhile, I'd suggest something like "delayed self-invoked function" (DSIF). –  Oct 25 '14 at 21:00