I've got a page where I want users to add themselves to a sharepoint group and then read the contents of the library. This requires three things, get the current user, add to group, and read the library. I've got the code working (shortened here for readability.) I'm using jQuery ajax to handle the REST calls.

function getUser(){
    var obj ={ "url":"etc..." } 
    return restCall(obj);   

function addUserToGroup(groupName){
    return getUser()
                    var obj ={"url":"add user endpoint"};
                    return restCall(obj);

function restCall(obj){
    return $.ajax(obj).fail(handleFail);    

And it is invoked like this

addUserToGroup('Temporary approvers')

The problem is that it waits for the getUser call to resolve, but then the addToGroup and requestLibraryItems both fire off at the same time. If the library read completes first they get no items because they weren't in the group.

I want each call to wait until the previous one has resolved. Any ideas how to chain all this? I'm sure it is something simple, but I'm just not seeing it.

  • 2
    I don't have time to put together a full answer but you should lookup jQuery Deferred Objects and Promises and the then() method. Basically it's callA().then(callB).then(callC). – Rob Windsor Mar 3 '16 at 21:46
  • As explained by Rob. I sometimes get lazy and simply put second ajax call into first one's success function. Which also works. – Charles Mar 3 '16 at 22:05
  • Thanks Rob. I think I had to many done() and needed more then(). Seems to be working correctly now. – Rothrock Mar 3 '16 at 22:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.