无法从完成的处理程序返回承诺

时间:2018-08-17 13:30:15

标签: jquery promise

我已经阅读了多篇有关从诺言处理程序返回诺言来解决数据依赖性的文章。但是,当我运行以下代码而不是完成的处理程序返回另一个承诺foobar时,它仍然包含第一个承诺的结果,就好像它忽略了我的返回:

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
});

我正在尝试实现我读过的nested promisesstackoverflowpromise chaining,但是到目前为止,即使我用$ .when包装pwaRestCall,也没有任何效果,我认为等同于promise.all,或附加完成的处理程序。

1 个答案:

答案 0 :(得分:1)

是的,isweekday 确实忽略了您的返回值。您永远不要使用done方法。改为使用done(这将返回一个新的链接承诺),它将按预期工作。