0

I am trying to break a nested for loop inside a asynchronous call back but unable to do so:

function asyncCall(id, OnComplete) {
    // id..
    context.executeQueryAsync(OnSuccess, OnFailure);

    function OnSuccess(sender, args) {
        OnComplete(userInGroup);
    }

    function OnFailure(sender, args) {
        console.error("Doesn't Exist!")
    }
}

function callApi() {
    //response from intial call
    for (var key in response) {
        var data = response[key];
        (function (innerData) {
            if (innerData) {
                renderHTML(innerData);
            }
        })(data);
    }
}

function renderHTML(data) {
    for (var key in data) {
        var index = data[key];
        (function (innerData, id) {
            asyncCall(id, function (isFound) {
                if (isFound)
                    break; //break loop
            });
        })(data, index);
    }
}

callApi();

I want to break the loop if the value of the property isFound is true in its response and want to achieve this in ES5 only or any work around like synchronous call might help.

5
  • 1
    You can't with the structure you show unless you make the asyc calls synchronous. As shown, the entire loop will probably have fully ran before the first asyc call resolves. Commented Nov 12, 2018 at 15:33
  • 1
    This looks like Javascript breaking a for loop inside a callback function Commented Nov 12, 2018 at 17:22
  • @Shilly thanks for the knowledge, how can i call this asynchronous method synchronously then? Commented Nov 13, 2018 at 7:09
  • @AsadShah Instead of caliing asyncCall inside a loop, wait for the response to arrive and then call the next one. But depending on what executeQueryAsync does, you might be able to leave it async. Need more information for that thoguh. Commented Nov 13, 2018 at 9:22
  • @Shilly executeQueryAsync takes id as parameter and fetches data from the server and returns boolean if the user exists in a group or not but i have to leave it async as it is the function of third party library. calling function asyncCall sequentially will help but don't know exactly how to call it sequentially. Commented Nov 13, 2018 at 10:40

2 Answers 2

0

You can't.

The loop will have finished before the break gets reached.

The only way to do this would be to run each invocation of asyncCall in series instead of in parallel. (e.g. by calling the next one from the callback function passed to the previous one).

Sign up to request clarification or add additional context in comments.

1 Comment

"The only way to do this would be to run each invocation of asyncCall in series instead of in parallel". Can you show it with the help of some example ?
0

As Quentin said, you can't.

But if you want to prevent further callbacks from happening, you could set a variable which prevents the callbacks to happen.

let isFound = false;

function asyncCall(id, OnComplete) {
  // id..
  context.executeQueryAsync(OnSuccess, OnFailure);

  function OnSuccess(sender, args) {
      if(isFound) return;
      isFound = true;
      OnComplete(userInGroup);
  }

  function OnFailure(sender, args) {
      console.error("Doesn't Exist!")
  }
}

2 Comments

At the moment i am doing exactly what you said, but don't want unnecessary calls to hit.
hit the onSuccess function? or the api?

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.