How to resolve a recursive promise in JavaScript?

How to resolve a recursive promise in JavaScript?

If an async function returns either another async function call or a Promise, the function/promise will resolve before the original call resolves, exactly the same way that resolving a Promise would in the Promise pattern. So, you can call getRedirectUrl (someUrl).then (…).catch (…) exactly the same way the original question would have.

When to use recursion in a promise chain?

As said above, all the promises in that bulk are in the end resolved with the same value 2, so all we would need is to store the outermost and the innermost promise at one time. All intermediate promises may become garbage-collected as soon as possible, and we want to run this recursion in constant space and time.

What’s the difference between setInterval and recursive setTimeout?

The other distinction between a setInterval call and a recursive setTimeout call is that setInterval delivers its function to the call stack regularly regardless of the status of its previous function calls, while setTimeout will schedule its next call only when its function runs and the new setTimeout is scheduled.

How to make a promise from setTimeout stack?

Usually you’ll have a promise library (one you write yourself, or one of the several out there). That library will usually have an object that you can create and later “resolve,” and that object will have a “promise” you can get from it. Then later would tend to look something like this:

Which is more concise, a recursive function or a promise?

A recursive solution to this problem is much more concise: This version of the code is structurally much simpler, without the need for any variables to maintain the current iteration state. With a recursive function, this same information is stored in the stack.

How to compare asynchronous recursion with promise and callback?

And running the complete example: Much the same as with the callback implementation, you can breakpoint the termination condition and see the async call stack. Comparing the promise version of this function to the callback implementation, the code is a little cleaner and easier to follow.

Which is the secret sauce of a recursive promise?

The secret sauce is the sub Promise whose’s successful completion will trigger a call to resolve () from the parent promise. As another illustration of recursive Promises, I used it to solve a maze.