Javascript 我想写一个函数-fetch_cached(),它将签入本地

Javascript 我想写一个函数-fetch_cached(),它将签入本地,javascript,arrays,url,Javascript,Arrays,Url,我想写一个函数-fetch_cached,它将签入本地。否则,请使用axios get下载链接并将结果保存在本地存储器中。有不同的解决方案。据我说,这应该运行良好 const fetch_cached = (url,key) => { axios.get(url).then(response => { let data = response.data; caches.has(key).then(bool => { if(false) {

我想写一个函数-fetch_cached,它将签入本地。否则,请使用axios get下载链接并将结果保存在本地存储器中。

有不同的解决方案。据我说,这应该运行良好

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和键。

欢迎使用堆栈溢出!请给我们您尝试过的代码,并阅读,以帮助我们更好地回答您的问题。