【问题标题】:Cannot return a promise from done handler无法从完成处理程序返回承诺
【发布时间】:2019-01-24 13:30:55
【问题描述】:

我已经阅读了多篇关于从 Promise 处理程序返回 Promise 以解决数据依赖关系的文章。但是当我运行以下代码而不是 done 处理程序返回另一个承诺时,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 也没有任何效果,我认为这相当于承诺。全部,或附加完成的处理程序。

【问题讨论】:

  • .then 指的是您的 sharepointRestCall。我可能建议在 pwaRestCall 之后添加 .then

标签: jquery promise


【解决方案1】:

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

【讨论】:

猜你喜欢
  • 2017-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-22
  • 2020-09-21
  • 2019-02-06
相关资源
最近更新 更多