0

Here is my code, I want to run the loop until i<2*(actors.length-1)

async function drawArrows(actors, timeout, drawArrow, i = 0) {
    drawArrow(i, timeout, actors.length-1);
    for(const actor of actors){
        new Promise(function(resolve, reject){
            setTimeout (() => {
                resolve(drawArrows(actor,timeout,drawArrow, ++i));
            },timeout) ;
        });
    };
};

Where should this i<2*(actors.length-1) be fit for terminating this loop?

tsm009
  • 39
  • 1
  • 9
  • 6
    Why `async` when there's no `await`? – Andreas Oct 12 '20 at 07:39
  • 1
    Does this answer your question? [Asynchronous Process inside a javascript for loop](https://stackoverflow.com/questions/11488014/asynchronous-process-inside-a-javascript-for-loop) – Sajeeb Ahamed Oct 12 '20 at 07:39
  • There is no need to async here imho, because there is nothing async happening. Just do a recursive function that executes itself inside the timeout. – briosheje Oct 12 '20 at 07:40
  • @Andreas how this promise will be convert in await? Is it `cont act = await Promise.resolve(drawArrows(actor,timeout,drawArrow, ++i))` .... – tsm009 Oct 12 '20 at 07:42
  • @SajeebAhamed no. I have to use for loop inside this `drawArrows` function. And also how to remove this recursive call and do it with await method? – tsm009 Oct 12 '20 at 07:46
  • `Where should this` before the for loop – Jaromanda X Oct 12 '20 at 07:50
  • ```async function drawArrows(actors, timeout, drawArrow, i = 0) { const j = 2*(actors.length-1); drawArrow(i, timeout, actors.length-1); for(let i=0; i < j; ++i){ await drawArrows(actors,timeout,drawArrow); }; };``` Changing the above. Loop is not executing with condition `i < j` – tsm009 Oct 12 '20 at 07:54
  • @JaromandaX `drawArrows may call an async function and await it to * be ready` this is what I want to do using `for loop`. – tsm009 Oct 12 '20 at 07:59
  • when I said before the loop, I meant before the first line of the function - seeing as the first line of drawArrows calls drawArrows, you'll need to terminate somehow before that line otherwise what you got there is an infinite recursion - – Jaromanda X Oct 12 '20 at 08:01

0 Answers0