Introduction
SharePoint 2013 Client Object Model is used to retrieve, update and manage the data in SharePoint 2013 library/List. SharePoint makes an Object model available in several forms but here, we are using Javascript Object Model.
- JavaScript library(JSOM)
- REST/OData endpoints
In this Javascript Object Model, we will use executeQueryAsync() but it will execute/ will not wait for success/ fail to complete. We need to make executeQueryAsync() behave synchronously.
It means the function will wait for async operations, which should be completed and should return some values.
We know that SharePoint JavaScript Client Object Model is asynchronous. However, sometimes we want to process the things in a synchronous way.
This can be done by using JavaScript callbacks and deferred/Promises. Let's first see an asynchronous example, which we will later convert to synchronous:
For this reason, JavaScript is having an option to use deferred/promise method do this operation.
Promise
The promises pattern significantly simplifies JavaScript code when you make an app, which must have multiple, nested asynchronous calls and it makes it very powerful.
Example
Your JavaScript code has to update multiple list items one by one to complete the basic operations and wait for the first one to execute.
However, Promise object also acts like a caching mechanism, where the success or failure function will be called immediately, if the Promise has already been fulfilled.
Promises object can make it easy to execute dependent asynchronous calls sequentially, which are based on the results.
Syntax
The deferred/ Promise object is very simple syntax and powerful method for sequentially updating in the list/ list items.
Initially, we need to declare the deferred object, as shown below.
- var deferred = $.Deferred();
At the end of the function, we have to return Promise object.
- return deferred.promise();
First, we will see about normal JavaScript execution.
- <script type="text/javascript">
- var 0camlItems;
- $(document).ready(function () {
- //don't exectute any jsom until sp.js file has loaded.
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getvalues);
- });
- function getvalues() {
- retrivethelistitem('Tutorial list');
- console.log(“Execute second after the retrieve list items ”);
- }
- function retrivethelistitem(listTitle) {
- var clientContext = new SP.ClientContext.get_current();
- var olist = clientContext.get_web().get_lists().getByTitle(listTitle);
- var camlQuery = new SP.CamlQuery();
- ocamlItems = olist.getItems(camlQuery);
- clientContext.load(ocamlItems);
- clientContext.executeQueryAsync(
- Function.createDelegate(this, this.success),
- Function.createDelegate(this, this.failure)
- );
- };
- function success(sender, args) {
- var listItemEnumerator = ocamlItems.getEnumerator();
- while (listItemEnumerator.moveNext()) {
- var olistItem = listItemEnumerator.get_current();
- console.log(‘execute first ‘);
- console.log(olistItem.get_item('Title'));
- }
- }
- function onQueryFailed(sender, args) {
- console.log('An error occured while retrieving list items:' + args.get_message());
- }
- </script>

Wes WilliamsPosted Jul 7, 2020, 8:50 AM
We are on SP19 onpremise and trying to find modern search code. any suggestions https://github.com/pnp/sp-dev-solutions/tree/master/solutions/ModernSearch/react-search-refiners
Gigino GigettoPosted Jul 18, 2018, 9:41 AM
I think there is a mistake at line 46...it would be "failure" instead onQueryFailed?
Souvik ChakrabortyPosted Sep 8, 2017, 3:50 AM
Thanks for this article. One small update. I got a variable scope error in SP 2010 and then I declared the var deferred=$.Deferred(); globally. After that it worked,
Gowtham RajamanickamPosted Mar 21, 2017, 1:33 AM
Thank you all for comments......
Manav PandyaPosted Mar 20, 2017, 9:47 AM
Thanks for sharing .....
Sagar PardeshiPosted Mar 13, 2017, 2:25 PM
Nice concept explore