Call asynchronous function recursively

maryum375

I have an asynchronous function that I want to call multiple times in a row. The problem is that "multiple" can be a few hundred thousand or millions...

The obvious way is to call the same function from the callback like that:

function foo()
{
    asyncBar(foo);
}

Of course some logic is involved to stop the recursion. The question is whether the stack is filling with calls and may cause stackoverflow at some point?

jfriend00

The question is whether the stack is filling with calls and may cause stackoverflow at some point?

No. If asyncBar() calls the callback it is passed asynchronously, then there is no stack build-up.

In your code:

function foo() {
    asyncBar(foo);
}

here is what is happening, step-by-step:

  1. First foo() is called.
  2. This then calls asyncBar(foo).
  3. Because asyncBar is asynchronous, that means it starts an asynchronous operation (let's suppose it is an http GET, but any async operation will do). That asynchronous operation is initiated, but then asyncBar() immediately returns.
  4. That initial call to foo() returns and the stack is completely unwound. There is no foo() on the stack any more.
  5. Any code after the call to foo() continues to run until it is done and returns back to the event loop.
  6. Meanwhile the asynchronous operation finishes some time in the future. That places a call to your callback in the event queue.
  7. When the JS engine is done executing other Javascript (which means the stack is completely empty), it pulls that event out of the event queue and calls the callback.
  8. In this case, the callback function is foo so it calls that function and starts the cycle all over again, right back to step 2.

There is no stack build-up. The key is that asynchronous callbacks are called sometime later, after the current stack has finished, unwound and returned back to the system.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Asynchronous function call is not working, synchronous is working

How to call an asynchronous JavaScript function?

Call An Asynchronous Javascript Function Synchronously

How to recursively call a function for a set period of time?

Can a lambda function call itself recursively in Python?

Functional js - cant recursively call own function

How to call promise function recursively

Kotlin - How to recursively call a lambda function

Need to call table function recursively

Recursively calling an asynchronous API call

Call an asynchronous function with react

Call asynchronous function inside constructor

Call function recursively without additional parameter

why cannot recursively call function in jquery?

Is it ok to recursively call a directive's link function?

Wrap Asynchronous Function Call In an NSOperation

Argument lifetime of an asynchronous function call

Recursively calling asynchronous function that returns a promise

How to recursively call an asynchronous function?

Waiting for Asynchronous function call to complete

How to do asynchronous function call in vba

Asynchronous function call conflicts with deinitialization of an unowned object

Asynchronous Function Call

Nodejs Asynchronous reusable function call

How to asynchronous call function with the result of foreach loop

Call a function asynchronous in a synchronous code-base

Recursively call function to get last record in a chain

Is it a bad practice to call a function recursively on error

Not able to call a function recursively in Powershell