【发布时间】:2018-08-14 02:30:56
【问题描述】:
我对 Typescript 中的 Promises 和 Async 还很陌生,我仍在尝试了解实现已定义功能的异步逻辑的最佳方法。
我想编写一个函数,其中包含一个循环,该循环将触发多个 AJAX 请求。对于等待所有 AJAX 请求完成然后返回数组的最佳方式,我有点难过。我想出了以下实现,但我想知道这种方式是否是最好的方法。我还想知道是否其中一个 AJAX 请求失败并进入catch 块,这是如何处理的? Array 是否有用于该特定迭代的 null 对象?
public async MultipleAsyncCalls(): Promise<Promise<ItemInfo>[]> {
let resultSet = new Array<Promise<ItemInfo>>();
for (let item of ArrayOfItems) {
try {
let ajaxResult = $.ajax(<JQuery.AjaxSettings>{
url: "GetInformation" + "/" + item.Id,
method: 'GET'
});
resultSet.push(Promise<ItemInfo><any>ajaxResult);
} catch (ex) {
this.ShowNotification("Something went wrong with the AJAX result");
}
}
return resultSet;
}
public async ExampleCall(controllerConfigs: Promise<IControllerConfiguration[]>): Promise<ItemInfo[]> {
//This first await will be complete near instantly as it is the function call completion
//promise and not the AJAX completion.
let promiseArray = await this.MultipleAsyncCalls();
//Wait for all of the AJAX promises to complete.
Promise.all(promiseArray);
//Is it correct to assume here that all promsies of Array are done and this next loop will be near instant?
let itemArray = new Array<ItemInfo>();
for (let itemPromise of promiseArray) {
itemArray.push(await itemPromise);
}
return itemArray;
}
【问题讨论】:
标签: javascript typescript asynchronous promise