javascript - 迭代地读取和处理 chrome.storage 中的对象

标签 javascript google-chrome asynchronous google-chrome-storage

我将 blob 对象存储在 chrome.storage.local 中,并增加键“0”、“1”、“2”等。对象的数量不固定。从“0”开始迭代读取和处理它们的最佳方法是什么? (在前一个对象未​​被处理之前,我无法读取下一个对象)。

我知道可以使用此代码获取所有对象:

chrome.storage.local.get(null, function(items) {
    //handle items
});

但就我而言,可能有很多 blob 文件,我不想一次在内存中处理它们。

最佳答案

所以你只需要一个简单的循环,当你完成时,它会加载网络项目。简单队列类型的系统。

(function() {

    var ind = 0;
    function getNextItem() {
        var data = chrome.storage.local.get("key" + ind, processItem);
        ind++;
    }

    function processItems(items) {

        if(!items) return;

        //do stuff here when done call next 

        getNextItem(); // call this whenever your processing is done

    }

    getNextItem();

}());

关于javascript - 迭代地读取和处理 chrome.storage 中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41122060/

相关文章:

javascript - 如何从 Google Chrome 扩展重新呈现页面?

html - 图标与 Chrome 中的文本没有很好地对齐

html - 空格后的方 block (标签 : -Area-Undefined Area-WebKit WebKit-Fonts)

c# - “The Task Was Cancelled” HTTP客户端默认超时值

javascript - 如何获取页面上除div之外的所有元素

javascript - 倍数订单数量x价格并显示小数点后两位

objective-c - 如何使用 xCode 6 中的期望设置异步单元测试?

asynchronous - Clojure 的 core.async 是否类似于 Jane Street 的 OCaml Core Async?

javascript - Paper.js 在 Angular 上再添加 2 个点以增加高度

javascript - 如果打开或关闭 Accordion ,则交换 jQuery Accordion 的标题