【发布时间】:2016-05-10 20:24:17
【问题描述】:
我正在 SharePoint 中构建一个需要发出三个单独的 AJAX 请求的应用程序:
- 为帐户 ID 具有特定职位的所有人 (searchUsers) 调用 Search API。
- 使用在 searchUsers (getUserProps) 中检索到的每个帐户名调用用户配置文件 API。
- 使用在 getUserProps 中检索到的经理帐户名称(第二个 getUserProps),为每个用户的经理姓名再次调用 User Profile API。
我在上面用括号括起来的三个操作是我正在编写的自定义库的一部分,以减轻一遍又一遍地编写 ajax 对象的繁重工作。不知道你们都需要看到多少库,我不想用一堆代码来解决这个问题,所以我在this jsfiddle 中添加了库的操作部分。
所以,这主要是因为我对延迟承诺和 ajax 的来龙去脉(通常可能还有整个语言)缺乏了解。但如果可能的话,我想在没有一堆嵌套函数的情况下完成上述操作。或者想知道实现这一目标的最佳实践/最有效的方法。
更新
根据 Vadim Gremyachev 的建议,我采用了jQuery.when 方法:
searchPeople().then(function(result){
//prepare promises
var promises = result.items.map(function(q){
return getUserProps(q.Manager);
});
//resolve for each of the promises
$.when.apply($, promises).then(function(){
console.log('Promises resolved.')
});
})
.fail(function(error){
console.log(JSON.stringify(error));
});
function searchPeople(){
var api = new ApiHelper('https://sharepoint/sites/subsite');
var options = {
query:'Job Title',
props:['Manager']
};
return api.searchUsers(options);
}
function getUserProps(account){
var api = new ApiHelper('https://sharepoint/sites/subsite');
var encName = encodeURIComponent(account);
return api.getUserProperties(encName);
}
您会注意到,在$.when.applyline 中,我刚刚收到了控制台喊声。这是因为我无法得到解决的承诺。我可以在开发人员工具中看到我所有的 getUserProperties 调用都在运行。但他们永远不会解决,“承诺已解决”的调用永远不会发生。
帮助?
【问题讨论】:
-
您应该始终使用
then而不是done,以允许链接。