我有一个关于在将值返回给客户端之前等待承诺解决的问题。
我正在使用 SharePoint 框架从我的配置列表中获取列表和文档库名称的集合,然后我使用这些名称(列表和文档库名称)回调 SharePoint 以检索每个列表项列表和文档库。
下面提供的是我的代码来检索我将用来检索列表项的列表和文档库的名称:
private _getListData(): Promise<ISPLists> {
let listName:string = "SPListDisplayUIConfig";
let currentUrl = this.context.pageContext.web.absoluteUrl + `/_api/web/lists/GetByTitle('`+listName+`')/Items`;
var formatedResponse: ISPLists = { value: [] };
return this.context.spHttpClient.get(currentUrl, SPHttpClient.configurations.v1)
.then((response: SPHttpClientResponse) => {
//Extract the response.json object.
return response.json().then((responseFormated: any) => {
//Evaluate the response.
responseFormated.value.map((object: any, i: number) =>{
var listItems: ListObjects = { value: [] };
var spListItem: ISPList = {
Id: object['ID'],
Title: object['Title'],
DisplayListTitle: object['DisplayListTitle'],
RowCount: object['RowCount'],
SortOrder: object['SortOrder'],
ShowOnHomePage: object['ShowOnHomePage'],
ListType: '',
ListItems: listItems
};
formatedResponse.value.push(spListItem);
});
//Create a local list and store all the this._getListItems(Title) calls into the array, then pass the local array into the Promise.all method to execute each
//method. In the return result (then), extract each list item and store into the spListItem.ListItem property.
let promisesMethods:any = [];
formatedResponse.value.forEach((item:ISPList) => {
let listName:string = item.Title;
promisesMethods.push(this._getListItems(listName));
});
Promise.all(promisesMethods).then((result:any) => {
console.log("Here");
});
console.log("Here 1")
return formatedResponse;
})
})
}
this._getListItems 方法检索每个列表名称的列表项。
let promisesMethods:any = [];
formatedResponse.value.forEach((item:ISPList) => {
let listName:string = item.Title;
promisesMethods.push(this._getListItems(listName));
});
Promise.all(promisesMethods).then((result:any) => {
console.log("Here");
});
console.log("Here 1")
这是 this._getListItems 定义:
private _getListItems(ListName: string): Promise<ListObjects>{
let currentUrl = this.context.pageContext.web.absoluteUrl + `/_api/web/lists/GetByTitle('`+ListName+`')/Items`;
return this.context.spHttpClient.get(currentUrl, SPHttpClient.configurations.v1)
.then((response: SPHttpClientResponse) => {
return response.json().then((responseFormated: any) => {
var formatedResponse: ListObjects = { value: []};
responseFormated.value.map((object: any, i: number) =>{
var spListItem: ListObject = {
Title: object['Title'],
ItemUrl: "",
ItemType: "",
FileType: "",
Id: object['ID']
};
formatedResponse.value.push(spListItem);
});
return formatedResponse;
})
})
}
在下面的代码中,console.log("Here 1") 在 console.log("Here") 之前被调用。在将结果返回给客户端之前,我需要获取每个列表和库的所有列表项。
let promisesMethods:any = [];
formatedResponse.value.forEach((item:ISPList) => {
let listName:string = item.Title;
promisesMethods.push(this._getListItems(listName));
});
Promise.all(promisesMethods).then((result:any) => {
console.log("Here");
});
console.log("Here 1")
return formatedResponse;
谢谢