-1

Is there a possibility to make some delay? I call a function in a while loop. This function calls executeQueryAsync which has to finish before the loop continues. When I use an alert my code works but without it doesn't.

    while (listPermsEnumerator.moveNext()) {
        enumG = groups.getEnumerator();
        var rAssignment = listPermsEnumerator.get_current();
        var member = rAssignment.get_member();
        var groupCounter = 1;
        var name = '';

        //alert(''); This alert makes code work

        while (enumG.moveNext()) {
            var group = enumG.get_current();
            var groupname = group.get_title();
            //alert(groupname);
            if (member.get_title() === groupname) {
                name = groupname;
                SP.SOD.executeOrDelayUntilScriptLoaded(function(){
                    retrieveAllUsersInGroup(groupname, groupCounter, groups);
                }, key);
            }
            groupCounter++;
        }

        roleAssignment = this.listRoleAssignments.getByPrincipalId(member.get_id());
        roleBindings = roleAssignment.get_roleDefinitionBindings();
        // in checkPermission() another executeQqueryAsync is called
        checkPermission(context, roleAssignment, roleBindings, name);
    }

    ...
    function checkPermission(context, roleAssignment, roleBindings, name) {
        this.name = name;
        context.load(roleAssignment);
        context.load(roleBindings);
        context.executeQueryAsync(Function.createDelegate(this, Bind), Function.createDelegate(this, BindFail));
    }
  • Please show us the code you have, we can't help you without. – Zim84 Sep 24 '15 at 14:26
  • found solution using jquery promises/diferred [here](http://blog.qumsieh.ca/2013/10/31/using-jquery-promises-deferreds-with-sharepoint-2013-jsom/) – sairfan Feb 12 '18 at 18:32

2 Answers2

1

The simplest solution would be to code your methods in a way that reflects the purpose of asynchronous operations. You seem to be trying to work around the ExecuteQueryAsync and trying to "make" it synchronous.

Here is a similar example -- see the 2nd answer: ( https://sharepoint.stackexchange.com/questions/95907/executequeryasync-in-for-loop ) Basically you a) write the callback function inline, and b) you put the loop in the success callback.

(What's great about writing the "success" callback function in line is the success callback function then has access to all the variables in the method. It's a closure).

Community
  • 1
  • 1
B.C.
  • 83
  • 1
  • 8
0

If you need to loop through an array of asynchronous jobs, you can do something like this:

var reports = [11, 12, 14, 15];
function doTheReport() {
    if (reports.length === 0) {
        alert('All reports are done now.');
        return;
    }

    var report_Id = reports.pop();
    $.ajax({
        url: "/DoTheReport",
        complete: function () {
            doTheReport();
        }
    });
};
Catalin
  • 11,503
  • 19
  • 74
  • 147
  • I retrieve SharePoint Groups and need to request users of each group. This kind of request is afaik only possible with "executeQueryAsync" function which I need to call for each user in the loop. – pyTh0n-M4nning Sep 24 '15 at 15:41