USE OF DEFERRED OBJECT IN SHAREPOINT JSOM

In previous post I explain use of Deferred Object in SharePoint REST API. In this post I’m going to explain use of Deferred object in JSOM/CSOM.

To use client context in the JavaScript you need to as below files in your page or masterpage as reference which is available in /_layouts/15/ folder.

  • inheritMicrosoftAjax.js
  • sp.runtime.js
  • sp.js

JQuery Deferred object can be used here too to make things simple, we have created a small example for that too.

functionToCall().done(function (returnedValue) {
     // do something with the returned value
});

function functionToCall() {
     var dfd = $.Deferred(function () {
     var valueToReturn = clientContext.get_web();
     clientContext.load(valueToReturn);
     clientContext.executeQueryAsync(function () {
               dfd.resolve(valueToReturn);
          },
          function (sender, args) {
               //throw an error
          });
     });
     return dfd.promise();
}

Hope this will help you and save your time..

Happy Coading… 🙂

Thanks, Jay Doshi