Is there a more elegant way to handle local storage API usage?

I am developing a browser extension, that uses local storage to store URLs.

the local storage is used in background script and in script for the popup and extension options.

Whenever a function needs these sites, it basically makes an API call to retrieve them.
But I was wondering, if there is a better way to get these data and not call the API from multiple functions all the time. (for example store it in a file or a variable)

for now, i retrieve data like this:

let whiteList = browser.storage.local.get("whiteList");
  whiteList.then((res) => {
    // check if there are any blacklisted sites
    if (!res.whiteList || res.whiteList.left < 1) {
      whiteListedSites = [];
      // parse blackListed sites to object
    } else {
      whiteListedSites = JSON.parse(res.whiteList);
    }

Thanks for your help! 🙂

119 thoughts on “Is there a more elegant way to handle local storage API usage?”

Leave a Comment