0

I want to get owner site title without async. How i can do?

var GetOwnerSiteTitle = function(){
    var context = new SP.ClientContext();
    var groupCollection = context.get_web();
    var group = groupCollection.get_associatedOwnerGroup();
    context.load(group);
    context.executeQueryAsync(function(){
        console.log(group.getTitle());
    }, function (){
        console.log('fail');
    }); 
};
2

It should be noted that there are no synchronous calls available in the JS Object Model, and this is on purpose. The proposal to use promises is just a different async pattern, but that's okay…

It is possible to artificially 'force' the scripts to work in a synchronous manner using various techniques, but there's really no reason to.

Async patterns (direct callback from executeQueryAsync or the various implementations of the promise pattern) are setup to perform the work you need to accomplish and allow your page to perform well without blocking the UI process -- that is the whole point. A traditional execution environment (an application running on the server/client machine) makes it possible to run many different tasks simultaneously, but working with JavaScript in the browser you only really get a single execution process to work with (web workers being the notable exception).

Working with executeQueryAsync is actually quite straight forward, it just follows a slightly different execution path when you’re reading down your code than using a synchronous model.

In general the pattern works like this:

  1. Load one or many client objects (SP.ClientContext.load(object)) to instruct SharePoint you want to retrieve one or many pieces of information.
  2. Instruct the framework to execute executeQueryAsync and provide two callbacks as parameters; the first for the case where the call succeeds, and the second in case the calls encounters an error
  3. Inside of your initial success callback, continue the optimal execution path – perhaps loading more client objects and calling executeQueryAsync again and providing success and failure handlers once again (these could be the same functions over and over, or separate functions, or anonymous functions defined inline)

The execution flow can get messy if you’re not careful, but does not need to be significantly more complicated than a standard synchronous format.

JS execution with the JSOM in SharePoint works quite well with the async patterns provided – I encourage people to give them a chance even though they’re not what you might be used to!

|improve this answer|||||
1

You should use jQuery Promises

http://www.shillier.com/archive/2013/03/04/using-promises-with-the-javascript-client-object-model-in-sharepoint-2013.aspx

doSomething()
.then(doSomethingElseWhenThatsDone, orHandleAnError)
.then(doYetAnotherThingWhenThatsDone, orHandleAnError);

function doSomething(){
   //set up some JSOM stuff
   var dfd = $.Deferred();
   context.executeQueryAsync(
    function () {
        dfd.resolve();
    }, function (sender, args) {
        dfd.reject(sender, args, errorMsg);
    });
   return dfd.promise()
}

https://stackoverflow.com/questions/22411220/how-do-i-get-the-executequeryasync-to-behave-more-synchronusly

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.