0

Right now I have funcA, funcB, arrayA and arrayB. In funcA, arrayB will populate itself by requesting some external information and the time for doing this is varied. I want it to execute funcB after arrayB.length == arrayA.length, and arrayB is a global variable that its content will be used in funcB. I assume that I need to use JQuery deferred and promise..so I tried this

     var arrayB = [];
     var hi = funcA();
     hi.then(funcB());

     funcA(){
            var Dfd = $.Deferred();
            arrayB.forEach(function(x, i){
                   some external retrieval;
                   if (arrayB.length == arrayA.length){
                         Dfd.resolve(arrayB);
                   }
             })

        return Dfd;
     }

But this doesn't help. How should I change it?

dwuuuu
  • 29
  • 1
  • The check needs to be done inside "some external retrieval", otherwise it just never resolves (since it only happens once) – MinusFour Jul 04 '17 at 23:29
  • @MinusFour my bad. It was inside that function. I just changed my post – dwuuuu Jul 04 '17 at 23:30
  • so, in funcA, arrayB must already have data in it? and arrayA is somehow magically populated with data by some means that you have not shown? – Jaromanda X Jul 04 '17 at 23:33
  • is `some external retrieval` asynchronous? – Jaromanda X Jul 04 '17 at 23:34
  • 1
    `hi.then(funcB());` does calling funcB return a function? because any *non-function* argument is ignored by `.then` ... and the argument you are supplying in this `.then` is the **result of calling funcB** – Jaromanda X Jul 04 '17 at 23:34
  • 1
    We could help you a LOT better if you include ACTUAL code including the asynchronous operation, whatever that is. We need to know when that is done and how we know that depends upon what it exactly is and whether it already uses promises. This question is too theoretical to get a great answer. Please show us your actual code for what you're trying to do and we can make a much better answer. FYI, showing actual code instead of a made up example is pretty much always better here. – jfriend00 Jul 05 '17 at 00:43
  • 1
    Probably, you want to promisify the async operation, use `Promise.all()` to know when they're all done and then return a promise that can be used by passing a function reference to `.then()` as in `.then(funcB)`, not the way you have it. But, we need to see more real code, including the async operation to know how to best recommend this. – jfriend00 Jul 05 '17 at 00:45

2 Answers2

0

arrayB.forEach won't do a thing. It's empty. Forget all this functions and deferred.

fetch('/mydata.json')
  .then(function(response) {
    //save your data from response to arrayB
    //call your funcA
  })
  .catch((error) => {
    console.log(error);
});

I don't know why you needed that:

if (arrayB.length == arrayA.length){
   Dfd.resolve(arrayB);
}

But feel free to make a check before calling funcA.

Angels
  • 230
  • 1
  • 8
0

I resolved the problem by doing this:

  hi.done(function(){funcA()});
dwuuuu
  • 29
  • 1