0

I have such a code :

 function onDoneFunction()
  {

    console.log("done!");

  }

  function generalQuery(generalArray, onDoneFunction)
  {

    function go(i)
    {

      if(i >= generalArray.length)
        {
          onDoneFunction();
        }
        else
        {
          iteratorFunction(generalArray[i], function()
          {
             console.log("entering callback " + i);
             return go(i + 1);
          });

        }
    }

    go(0);
  }

And my iteratorFunction looks like this :

function iteratorFunction(partofquery, callback)
{
   var index = generalArray.indexOf(partofquery);

   collection.find(partofquery).then(function(data)
   {
       console("query completed " + index);
   }
}

Supposing my query array has two elements, I see such outputs :

entering callback 0

entering callback 1

query completed 0

query completed 1

But I'm trying to see this :

entering callback 0

query completed 0

entering callback 1

query completed 1

I have been trying to make loop wait for an iteration to finish before starting the next one. I have tried many things. As you can see I have tried using recursion as well. But I can't achieve that. Could you please show me what I'm doing wrong? Thanks in advance.

6
  • .then - ahh, so you need to chain some promises by the look of it Commented Aug 12, 2017 at 8:17
  • @JaromandaX Could you show with an example using my code? Commented Aug 12, 2017 at 8:19
  • Possible duplicate of How do I return the response from an asynchronous call? Commented Aug 12, 2017 at 8:20
  • Note, you pass a callback to iteratorFunction but never call it - not that you need to since you have Promises to work with Commented Aug 12, 2017 at 8:23
  • @JaromandaX In actual code, I'm using the callback. I posted the simplified version here. Commented Aug 12, 2017 at 8:24

1 Answer 1

1

Simplest fix to your code

function onDoneFunction() {
    console.log("done!");
}

function iteratorFunction(partofquery, index) {
    return collection.find(partofquery).then(function(data) {
        console("query completed " + index);
    });
}

function generalQuery(generalArray, onDoneFunction) {
    function go(i) {
        if (i < generalArray.length) {
            console.log("performing query " + i);
            return iteratorFunction(generalArray[i], i).then(function() {
                return go(i + 1);
            });
        }
    }
    go(0).then(onDoneFunction);
}

However, recursion isn't the best solution - but you did ask for an example using my code

Using Array#reduce instead of recursion

function generalQuery(generalArray, onDoneFunction) {
    generalArray.reduce((promise, item, index) => 
        promise.then(() => 
            iteratorFunction(item, index)
        ), 
        Promise.resolve()
    ).then(onDoneFunction);
}
Sign up to request clarification or add additional context in comments.

12 Comments

Are onFunctionDone and onDoneFunction same?
wait - I messed up
So, I just need to cancel callbacks and use promises, am I right?
That's what I'd do, since you already are dealing with promises
I've just looked at your code again and realised some strange thing in iteratorFunction (all I did to it was add a return) ... var index = generalQuery.indexOf(partofquery); but generalQuery is a function? what do you expect this to do?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.