Synchronous programming behavior with JSOM in SharePoint Online ?

Brass Contributor

Hello,

This is my first post @network.office.com.Hope this is not gonna ignored.

 

I'm just trying to get some value and append it to a list when everytime when a asyncronous call of code.

 

Example:

function prepareTables() {
getItemsWithCaml('CustomMyList',
function (camlItems) {
var listItemEnumerator = camlItems.getEnumerator();
while (listItemEnumerator.moveNext()) {
var listItem = listItemEnumerator.get_current();
alert(listItem.get_item('Email'));
}
alert('Completed table prparation.');
},
function (sender, args) {
alert('An error occured while retrieving list items:' + args.get_message());
});
}

function getItemsWithCaml(listTitle, success, error) {
var clientContext = new SP.ClientContext.get_current();
var list = clientContext.get_web().get_lists().getByTitle(listTitle);
var camlQuery = new SP.CamlQuery();
var camlItems = list.getItems(camlQuery);
clientContext.load(camlItems);
for(var i=0;i<3;i++){
var a ='#';
clientContext.executeQueryAsync(
function () {

success(camlItems);
},
error
);
}
};
prepareTables();

 

I want to append value of a  3 time in alert(listItem.get_item('Email'));.

Means every time we get a value from list it append with # but our value is in getItemsWithCaml function.

 

 

Help Needed!

 

Thanks.

 

3 Replies

It's pretty hard to understand what you are trying to achieve.

 

What it looks like you are doing is executing the call to SharePoint three times and then iterating over the result three times. That's a bit weird as the call wil retreive the same results.

 

clientContext.load(camlItems);
    for (var i = 0; i < 3; i++) {
        var a = '#';
        clientContext.executeQueryAsync(
            function () {

                success(camlItems);
            },
            error
        );
    }

 

I want a function call back behaviour (like deferred and promises).

I have my values just before Async function.

I want to concatenate each value every time with SharePoint list data when I get a value.