Skip to content

Instantly share code, notes, and snippets.

@arjndr
Forked from dslounge/cached-fetch.js
Created May 6, 2020 08:31
Show Gist options
  • Select an option

  • Save arjndr/1ed94a191bb6066f492e3cf6413f19f1 to your computer and use it in GitHub Desktop.

Select an option

Save arjndr/1ed94a191bb6066f492e3cf6413f19f1 to your computer and use it in GitHub Desktop.

Revisions

  1. @dslounge dslounge revised this gist Oct 24, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cached-fetch.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    export const cachedFetch = url => {
    const key = `@benkyou:${url}`;
    const key = `@MySuperStore:${url}`;
    return AsyncStorage.getItem(key)
    .then(data => {
    if (!data) {
  2. @dslounge dslounge created this gist Oct 24, 2017.
    23 changes: 23 additions & 0 deletions cached-fetch.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    export const cachedFetch = url => {
    const key = `@benkyou:${url}`;
    return AsyncStorage.getItem(key)
    .then(data => {
    if (!data) {
    return Promise.reject("no data found in async storage");
    }
    console.log("found data on disk");
    return JSON.parse(data);
    })
    .catch(() => {
    console.log("didn't find data on disk, fetching");
    return fetch(url).then(response => {
    if (response.ok) {
    return response.json().then(data => {
    AsyncStorage.setItem(key, JSON.stringify(data));
    return data;
    });
    }
    return Promise.reject("unable to load data");
    });
    });
    };