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.

jason
  • 6,962
  • 36
  • 117
  • 198
  • `.then` - ahh, so you need to chain some promises by the look of it – Jaromanda X Aug 12 '17 at 08:17
  • @JaromandaX Could you show with an example using my code? – jason Aug 12 '17 at 08:19
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – StudioTime Aug 12 '17 at 08:20
  • Note, you pass a `callback` to `iteratorFunction` but never call it - not that you need to since you have Promises to work with – Jaromanda X Aug 12 '17 at 08:23
  • @JaromandaX In actual code, I'm using the callback. I posted the simplified version here. – jason Aug 12 '17 at 08:24
  • in that case, you've simplified your code so much that a proper answer would be difficult - hopefully the code in my answer is applicable – Jaromanda X Aug 12 '17 at 08:26

1 Answers1

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);
}
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87