Skip to content

Instantly share code, notes, and snippets.

@ayoola-solomon
Created February 22, 2018 14:28
Show Gist options
  • Save ayoola-solomon/d12ad064a83850360aa1ef2f0eea61eb to your computer and use it in GitHub Desktop.
Save ayoola-solomon/d12ad064a83850360aa1ef2f0eea61eb to your computer and use it in GitHub Desktop.
promise idea
export const fetchCache = (key) =>
new Promise(resolve => {
resolve(getCache(key)); // will return undefined is not available.
});
// in the action then you can do this
fetchCache(CLIENT_API.calculatedJson)
.then(data => {
if (data) {
return data;
}
return httpGet(CLIENT_API.calculatedJson);
})
.then(data => {
setCache(data.data);
dispatch(setCalculatedJson(data))
})
.catch(e => {
dispatch({
type: FETCH_CALCULATED_JSON_ERROR,
payload: { error: e.response.data || e },
});
console.error(e);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment