Stale-while-revalidate

Show the cached copy instantly, refresh in the background, then update the screen.

AJAX · Stale-while-revalidateAdvanced
const store = new Map();

async function swr(url, render) {
  if (store.has(url)) render(store.get(url), true);       // instant, maybe old
  try {
    const fresh = await (await fetch(url)).json();
    store.set(url, fresh);
    render(fresh, false);                                  // up to date
  } catch (e) {
    if (!store.has(url)) throw e;
  }
}
cacheswrfastui