javascript - 使用 Promise 进行分页的最佳方法是什么?

标签 javascript node.js recursion promise

我和我的 friend 正在按照 Promise 进行工作,我们确保在返回到最初的调用之前获取所有数据页。有没有更简单的方法来解决这个问题?

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); });

最佳答案

是的,您可以像这样链接 promise ,因为 documentClient.getDocuments 返回一个 promise 。

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); });

关于javascript - 使用 Promise 进行分页的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35279171/

相关文章:

javascript - 如何在 fabric.js 中获取目标元素的属性值

java - 我如何在Java中找到树中最长的单词(没有循环(for,while,do ...))

c++ - 相互递归类

javascript - 多级单选按钮

javascript - 将动态生成的元素保持在文档边界内

javascript - 使第一个子项和最后一个子项在 IE8 中工作

javascript - SDK API命名空间设计

node.js - 函数返回对象 Promise - nodejs

node.js - 如何在sequelize Nest.JS中添加钩子(Hook)

performance - 如何衡量循环方法和递归方法之间代码速度的差异?