【发布时间】: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 promises、stackoverflow、promise chaining,但到目前为止,即使我用 $.when 包装 pwaRestCall 也没有任何效果,我认为这相当于承诺。全部,或附加完成的处理程序。
【问题讨论】:
-
.then 指的是您的 sharepointRestCall。我可能建议在 pwaRestCall 之后添加 .then