micro-cacheable icon indicating copy to clipboard operation
micro-cacheable copied to clipboard

try/catch before saving to the cache

Open parweb opened this issue 7 years ago • 0 comments

Maybe it will be useful to not cache date if microFunction() throw an error

actual code

module.exports = (ms, microFunction) => async (req, res) => {
  const lastCall = await retrieveData(req.url)
  if (lastCall && lastCall.date > new Date()) {
    return lastCall.data
  }
  const data = await microFunction(req, res)
  const date = new Date(new Date().getTime() + ms)
  saveData(req.url, { data, date })
  return data
}

expected code

module.exports = (ms, microFunction) => async (req, res) => {
  const lastCall = await retrieveData(req.url)
  if (lastCall && lastCall.date > new Date()) {
    return lastCall.data
  }
  try {
    const data = await microFunction(req, res)
    const date = new Date(new Date().getTime() + ms)
    saveData(req.url, { data, date })
  } catch (e){
    console.error(e)
  }
  return data
}

parweb avatar Apr 09 '18 22:04 parweb