Skip to content

Instantly share code, notes, and snippets.

@olegpolyakov
Created September 15, 2019 08:08
Show Gist options
  • Select an option

  • Save olegpolyakov/90f7e6d433e43ab2d9c3c1c02ad96283 to your computer and use it in GitHub Desktop.

Select an option

Save olegpolyakov/90f7e6d433e43ab2d9c3c1c02ad96283 to your computer and use it in GitHub Desktop.
AJAX Cache
const form = document.querySelector('form');
const input = form.querySelector('input[type="search]"');
const cache = {};
form.addEventListener('submit', event => {
event.preventDefault();
const query = input.value;
if (!cache[query]) {
cache[query] = fetch(`https://google.com?search=${query}`);
}
cache[query]
.then(response => {})
.catch(error => {})
.finally(() => {});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment