-
-
Save ayoola-solomon/d12ad064a83850360aa1ef2f0eea61eb to your computer and use it in GitHub Desktop.
promise idea
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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