简体   繁体   English

从满足条件的存储中删除所有对象 - Chrome 扩展 JavaScript

[英]Remove all objects from storage meeting a condition - Chrome extension JavaScript

I want to write a function that returns a Promise that resolves after removing all objects from storage that have a property with a certain value, but to do that I need to call remove in a loop, which is asynchronous, so I don't know how I could safely resolve after removing everything.我想写一个 function 返回一个 Promise 在从存储中删除所有具有特定值的属性的对象后解析,但要做到这一点我需要在循环中调用 remove ,这是异步的,所以我不知道删除所有内容后如何安全解决。

Here is how I can resolve after removing one object:以下是删除一个 object 后我可以解决的方法:

function remove(value) {
  return new Promise(function(resolve, reject) {
    // Get all objects from storage
    chrome.storage.local.get(null, function(allStorage) {
      // Loop through keys
      for(let key in allStorage) {
        // Remove if the property === value
        if(allStorage[key].property === value) {
          chrome.storage.local.remove(key, function() {
            // Done removing one object
            resolve();
          });
        }
      }
    });
  });
}

But I want to remove all of them:但我想删除所有这些:

function removeAll(value) {
  return new Promise(function(resolve, reject) {
    // Get all objects from storage
    chrome.storage.local.get(null, function(allStorage) {
      // Loop through keys
      for(let key in allStorage) {
        // Remove if the property === value
        if(allStorage[key].property === value) {
          chrome.storage.local.remove(key, function() {
            // Done removing one object
          });
        }
      }
      // How can I safely resolve here, when all objects are removed?
      resolve();
    });
  });
}

After learning more about promises I ended up doing this.在了解了有关承诺的更多信息后,我最终这样做了。 I feel like there must be a way to make it cleaner though.我觉得一定有办法让它更干净。

function removeAll(value) {
  return new Promise(resolve => {
    let promises = [];
    // Get all objects from storage
    chrome.storage.local.get(null, allStorage => {
      // Loop through keys
      for(let key in allStorage) {
        // Remove if the property === value
        if(allStorage[key].property === value) {
          // Create a new promise that resolves when object is removed
          let promise = new Promise(resolve => {
            chrome.storage.local.remove(key, _ => {
              console.log('deleted object from storage');
              resolve();
            });
          });
          // Push to array of promises
          promises.push(promise);
        }
      }
      // Resolve the function return promise when all promises in array resolve
      Promise.all(promises).then(_ => {
        resolve();
      });
    });
  });
}

Oh apparently remove accepts an array.哦,显然 remove 接受一个数组。

function removeAll(value) {
  return new Promise(resolve => {
    // Get all storage
    chrome.storage.local.get(null, allStorage => {
      let keysToRemove = [];
      // Loop through all keys
      for(let key in allStorage) {
        // Get the object associated with key
        let object = allStorage[key];
        // Push to remove array if value matches argument
        if(object.property === value) {
          keysToRemove.push(key);
        }
      }
      chrome.storage.local.remove(keysToRemove, _ => {
        resolve();
      });
    });
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM