使用承诺进行分页的最佳方式是什么?
What is the best way to approach pagination with Promises?
我
和我的朋友正在处理承诺,我们确保在返回最初的调用之前获得所有页面的数据。有没有更微不足道的方法可以解决这个问题?
function getDocuments(startIndex, result, entries) {
startIndex = typeof startIndex !== 'undefined' ? startIndex : 0;
result = typeof result !== 'undefined' ? result : {};
entries = typeof entries !== 'undefined' ? entries : [];
// build our entries set with the result parameter
for(var i in result.items) {
try
{
var id = result.items[i].id;
var name = result.items[i].name;
var content = result.items[i].content;
var entry = { "id": id, "name": name, "content": content };
entries.push(entry);
}
catch(e) {
}
}
// return a promise that fulfills a promise that then returns either a promise or a result.
return new Promise(function(fulfill, reject) {
// fulfill the promise and resolve the value, we pass a recursive promise as the value.
fulfill(documentClient.getDocuments({ "startIndex": startIndex }).then(function(result) { // once our request is made, let's check the page count.
var startIndex = result.startIndex;
var pageSize = result.pageSize;
var totalCount = result.totalCount;
if (startIndex + pageSize <= totalCount) { // if our current position is not at the end of the pages, return a promise with our current data and our current entries.
return getDocuments(startIndex + pageSize, result, entries);
}
return entries; // otherwise our entries will bubble back up the stack and be resolved into the initial fulfill value.
}));
});
}
getDocuments().then(function(d) { console.log(d.length); });
我的调整:
function getDocuments(startIndex, result, entries) {
startIndex = typeof startIndex !== 'undefined' ? startIndex : 0;
result = typeof result !== 'undefined' ? result : {};
entries = typeof entries !== 'undefined' ? entries : [];
// build our entries set with the result parameter
// ...
// return a promise that fulfills a promise that then returns either a promise or a result.
return documentClient.getDocuments({ "startIndex": startIndex }).then(function(result) { // once our request is made, let's check the page count.
var startIndex = result.startIndex;
var pageSize = result.pageSize;
var totalCount = result.totalCount;
if (startIndex + pageSize <= totalCount) { // if our current position is not at the end of the pages, return a promise with our current data and our current entries.
return getDocuments(startIndex + pageSize, result, entries);
}
return entries; // otherwise our entries will bubble back up the stack and be resolved into the initial fulfill value.
});
}
getDocuments().then(function(d) { console.log(d.length); });
是的,你可以像这样链接承诺,因为documentClient.getDocuments
返回一个承诺。
function getDocuments(startIndex, result, entries) {
// ...
return documentClient.getDocuments({ "startIndex": startIndex }).then(function(result) {
// ...
return entries; // otherwise our entries will bubble back up the stack and be resolved into the initial fulfill value.
});
}
getDocuments().then(function(d) { console.log(d.length); });
在getDocuments
中有一个代码路径,该路径不以新承诺结尾
这是一个简单的例子
function defer(fn) {
setTimeout(fn, 100);
}
function promiseChain(i, msg) {
if (i <= 0) return msg; // end code path
return new Promise((res, rej) => {
defer(e => res(promiseChain(i - 1, msg)));
});
}
promiseChain(10, "Hello World!").then(data => console.log(data));
// "Hello World!" logged after ten 100ms timeouts
相关文章:
- 在localhost Dev Box上测试JSONP请求的最佳方式
- 为react组件传递道具的最佳方式
- 让Webpack管理Quirky AMD定义的最佳方式
- 在承诺链中处理早期回报的最佳方式
- 在ng重复循环中显示条件内容的最佳方式是什么
- 在phonegap中为android调用onload函数的最佳方式
- 链接两个网页或网络应用程序的最佳方式
- 什么's是连接供应商js文件的最佳方式
- 什么's是在javascript中迭代项的最佳方式
- 在node.js中编写单元测试的最佳方式是什么
- 在页面上记录数据并实现pushstate()的最佳方式
- 使用AJAX在Rails中提交动态表单的最佳方式是什么
- 什么'这是加载jQuery的最佳方式
- 将大数组(字符串和类型数组的混合物)存储到blob或文件中/从blob或文件检索大数组的最佳方式
- 在网站上显示.mov作为加载屏幕的最佳方式
- 使用Angular存储用户以前是否选中过复选框,然后再调用它的最佳方式是什么
- 以角度渲染表中数据的最佳方式
- 删除集合中旧邮件/帖子的最佳方式
- 显示全屏Ajax加载程序的最佳方式
- PHP和JS中表单验证的最佳方式