Skip to content

try/catch before saving to the cache #12

@parweb

Description

@parweb

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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions