Skip to content

Instantly share code, notes, and snippets.

@jcuffe
Created November 21, 2018 20:45
Show Gist options
  • Save jcuffe/8812663f669ce3f737e4af32fbf7b603 to your computer and use it in GitHub Desktop.
Save jcuffe/8812663f669ce3f737e4af32fbf7b603 to your computer and use it in GitHub Desktop.
axios caching
//
// Cache the response of an axios request
//
const cacheRequest = request => {
let cache = null;
return () => {
cache = cache || request.then(({ data }) => data);
return cache;
};
};
//
// Expensive requests used in multiple calculations
//
const getSystemCosts = cacheRequest(axios.get(endpoints.systemCosts));
const getAdjustedPrices = cacheRequest(axios.get(endpoints.marketPrices));
//
// Add adjusted price for each item for job cost calculation
//
const injectAdjustedPrice = types => {
return new Promise(resolve => {
getAdjustedPrices().then(prices => {
prices.forEach(({ type_id: id, adjusted_price }) => {
if (types[id] && types[id].outputs) {
types[id].adjusted_price = adjusted_price;
}
});
resolve(types);
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment