0

I've read multiple articles about returning a promise from a promise handler to resolve data dependencies. But when I run the following code instead of the done handler returning another promise foobar still contains the results of the first promise, it is as if it ignores my return:

function sharepointRestCall( type, endpoint, headers, data ){
        return $.ajax({
                    type: type,
                    url: url + endpoint ,
                    dataType: "json",
                    headers: headers,
                    data: JSON.stringify( data )
                });
}

function pwaRestCall( type, endpoint, headers, data ){
    return $.ajax({
                    type: type,
                    url: url + endpoint ,
                    dataType: "json",
                    headers: headers,
                    data: JSON.stringify(  data )
                });
}

var listColumns;
var permission;
var projectGuid;

/*Create a project for this item using information from Nomination REST TEST */

//Read the nomination list
sharepointRestCall(
                                    "POST",
                                    "lists(guid'318382F9-0A5B-45B7-80D1-9527C9327513')/RenderListDataAsStream", 
                                    { "accept": "application/json;odata=verbose", "Content-Type": "application/json;odata=nometadata"},
                                    {'parameters': { 'RenderOptions': 2, 'ViewXml': "<View><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + getQueryParam("Id")  +"</Value></Eq></Where></Query><ViewFields><FieldRef Name='Title' /><FieldRef Name='Business_x0020_Area' /><FieldRef Name='PM' /><FieldRef Name='DirectorName' /><FieldRef Name='Project_x0020_Description' /><FieldRef Name='RiskImpact' /></ViewFields><QueryOptions /></View>"}}
                                )
.done( function( nomDataList ){
    listColumns = nomDataList.Row[0];

//we need the sharepoint list results AND permission to modify before doing anything else
    return pwaRestCall( "POST", "contextinfo", { "Accept": "application/json; odata=verbose"}, "" );
}).then(function ( foobar){
    console.log(foobar); // <-- contains nomDataList instead of the result of pwaRestCall
});

I'm trying to implement what I've read nested promises, stackoverflow, promise chaining but nothing has worked so far even if I wrap pwaRestCall with $.when, which I think is the equivalent of promise.all, or attach a done handler.

Amine Harbaoui
  • 1,247
  • 2
  • 17
  • 34
Useless Intern
  • 1,294
  • 1
  • 10
  • 20

1 Answers1

1

Yes, done does ignore your return value. You should never use the done method. Use then instead (which returns a new promise for chaining) and it will work as expected.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • This is the correct answer. I thought done and then were equivalent, I wasted so much time on this. FYI for people in the future done vs then: https://stackoverflow.com/a/5436869/1026641 – Useless Intern Aug 17 '18 at 13:42