【问题标题】:I want write a function -fetch_cached() which will check in local我想写一个函数 -fetch_cached() 来检查本地
【发布时间】:2020-10-22 06:23:25
【问题描述】:

我想写一个函数 -fetch_cached() 来检查本地。否则,使用 axios get 下载链接并将结果保存在本地存储中。

【问题讨论】:

  • 欢迎来到 Stack Overflow!请告诉我们您尝试过的代码并阅读How to Ask page 以帮助我们更好地回答您的问题。

标签: javascript arrays url


【解决方案1】:

有不同的解决方案。在我看来,这应该可以正常运行。

const fetch_cached = (url,key) => {

  axios.get(url).then(response => {

    let data = response.data;

    caches.has(key).then(bool => {

      if(false) {

        console.log(bool);

        console.log("The data is present in localcache.")

      } else {

        console.log("Data is being stored in localstorage")

        localStorage.setItem(key,JSON.stringify(data));

        console.log(JSON.parse(localStorage.getItem(key)));

      }

    });
  }) .catch(error => {

      return error;
  });
}

fetch_cached(url,key);

在 fetch_fetched() 函数中使用您自己的 URL 和密钥。

【讨论】:

    猜你喜欢
    • 2023-02-02
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多