簡體   English   中英

用Promises進行分頁的最佳方法是什么?

[英]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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM