S
S
sasha2014-04-24 22:29:06
JavaScript
sasha, 2014-04-24 22:29:06

js promise how to work in recursion?

The bottom line is, there is a code that uses the RSVP promise module for node js.

promise.then(function(){
    (function theFunction(arg){
         if (!arg) {
              //вот тут надо выйти из рекурсии в следующий then
          } else {
               theFunction(sdf);
          }
    }(adsf))
}).then(function(){
        // продолжаем жить
    })

and here is the question: how, using recursion (namely, the one called theFunction), exit it to the next then only by condition? As far as I understand, after this then is executed, in which the function theFunction is registered and which returns nothing, the next then will be immediately executed, and I only need to call the next step by condition.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Keith, 2014-04-25
@madmages

rsvp:Chaining
In the first `then` you create and return another promise, inside which you do your `theFunction` recursion and when you are ready you do a resolve. And so on to the second `then`.

S
Sergey, 2014-04-25
Protko @Fesor

Monsieur knows a lot... asynchronous recursions.... Are you sure that you need a recursion there? Or asynchrony.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question