Skip to content

Minor: avoiding purely sync async functions #133

Open
@jasnell

Description

As a general best practice, to avoid unnecessary performance cost, purely sync async functions should be avoided.

For instance, from https://github.com/covidgreen/covid-green-backend-api/blob/current/lib/server.js:

server.addHook('onSend', async (req, res) => {
    res.header('Cache-Control', 'no-store')
    res.header('Pragma', 'no-cache')
    res.header(
      'Strict-Transport-Security',
      `max-age=${config.security.hstsMaxAge}; includeSubDomains`
    )
  })

The hook function is not doing anything asynchronous and therefore should not be an async function:

server.addHook('onSend', (req, res, done) => {
    res.header('Cache-Control', 'no-store')
    res.header('Pragma', 'no-cache')
    res.header(
      'Strict-Transport-Security',
      `max-age=${config.security.hstsMaxAge}; includeSubDomains`
    )
    done()
  })

The anti-pattern of using async functions with sync code is seen several places throughout the code.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions