Utilizing Promises in SharePoint 2013 Apps

SharePoint-2013-Hosting

When developing more complex applications to SharePoint 2013 REST API , you have to make some calls quiet often asynchronous . For example , the initial calm call may take the master data while filling subsequest call child data . The challenge with a couple of calls is that they need to happen in sequence , but each call is made asynchronously. So the only solution is to nest calls depending on the “success ” callback function . The following code shows an example of nested calm call where the first call takes the name of the current user account and return the nested call social feeds for the account name .

The challenge with the code above is that is can rapidly become unmaintainable. Multiple levels of nested asynchronous calls simply creates a pile of “spaghetti” code. What is needed is a mechanism to separate out the various asynchronous calls while still maintaining the dependency between them. That is the function of a promise.

A promise – also known as a deferred – is a pattern that returns an object immediately from an asynchronous call. This object will later be populated with the result of the asynchronous call, but its immediate return simplifies the code structure making it much more maintainable. Furthermore, promises provide a built-in caching mechanism so that the same query does not have to be run again if the promise has already been successfully fulfilled.

There are several techniques for implementing promises, but one of the easiest is to make use of the jQuery $.Deferred method. The $.Deferred method allows you to create a deferred, which can be immediately returned from an asynchronous call. The deferred object has resolve and reject methods, which are called on success or failure respectively. Using deferreds allows you to separate the JavaScript code that performs the asynchronous call. The following code rewrites the initial code listing into two separate libraries that implement the promise pattern.

Notice in the rewritten code how the two libraries follow the same pattern. The deferred is returned immediately and the resolve/reject methods are called for success and failure.

The deferred object exposes a “promise” object that has a method named “then”. The “then” method takes two arguments: the first argument is a success function, the second is a failure function. So the library above can be invoked easily using the following code.

The promises pattern significantly simplifies JavaScript code when you app must make multiple, nested asynchronous calls and that makes it very powerful. However, the promise object also acts like a caching mechanism in that the success or failure function will be called immediately if the promise has already been fulfilled. This opens up additional ideas such as creating arrays of promises that contain fulfilled data so that apps do not have to run queries that have already successfully executed.

Update: Note that the $.ajax method returns a promise, so there’s no need to explicitly return a promise unless you want to extend it with your own extra information.