I have a problem with excuting the script in the right order. I have a custom.js which is loaded at the top of the masterpage. The following function is included:

var results;    
var followers;
function OnSuccess()
     function excuted()

    var clientContext = new SP.ClientContext.get_current();
    var counts = 0;
    var test = results.getEnumerator();
    while (test.moveNext()) {
    var person = test.get_current();
    $("#followersCount").text('(' + counts + ')');

function getFollowers()
     var clientContext = new SP.ClientContext.get_current();
     var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

     // Get the people who are following the current user.
     peopleFollowingMe = peopleManager.getMyFollowers();

     results = peopleFollowingMe;
     clientContext.executeQueryAsync(OnSuccess, requestFailed)

No matter what i tru the getFollowers is always excuted later then the controller of the display template where i say the following:

if(followers.indexOf(encodedName) > -1)

What did i try?


Also i tried putting the whole output in a function(html output) in the excuted but it won't help.

So how do i let the display template wait till the followers variable is set.


Take a look at how to use promises with executeQueryAsync from this page: http://blog.qumsieh.ca/2013/10/31/using-jquery-promises-deferreds-with-sharepoint-2013-jsom/.

function successCallback() {

function failCallback() {
    this.d.reject("something bad happened");

function getList(name) {
    var d = $.Deferred();

    var clientContext = SP.ClientContext.get_current();
    var list = clientContext.get_web().get_lists().getByTitle(name);

    var o = {d: d, list:list};
    clientContext.executeQueryAsync(Function.createDelegate(o, successCallback), Function.createDelegate(o, failCallback));

    return d.promise();

var p = getList("Documents");
p.done(function(result) {
    // result is an SP.List because that is what we passed to resolve()!
    var list = result;
    // do something with the list
p.fail(function(result) {
    // result is a string because that is what we passed to reject()!
    var error = result;
| 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.