Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

339
Vistas
What does `async` mean in JavaScript when it is *not* followed by `function`?

I stumble upon this code snippet while working on fastify.

module.exports = async function (fastify, opts) {
  fastify.register(proxy, {
    upstream: 'htt‌ps://news.ycombinator.com/',
    async preHandler(request, reply) {
      if (request.query.token !== 'abc') {
        throw fastify.httpErrors.unauthorized();
      }
    },
  });
};

Looks like it calls fastify.register with two parameters and the second parameter is an object. The first field is upstream but I don't get what comes next. What is this syntax after async preHandler(request, reply)...?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

fastify.register seemingly has this signature: fastify.register(proxy, {});. So the second parameter is an object.

As stated in MDN and by Andreas in comments you can have an object that contains a method signature. So your object can be declared as:

{
   upstream: string,
   preHandler(request, reply){}
}

Where preHandler(request, reply){} is essentially a shorthand for preHandler: function(request, reply){}

You can then declare this method signature async (again as stated in docs). Which results in:

{
   async preHandler(request, reply){}
}

The above could also be declared as:

{
   preHandler: async function(request, reply){}
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda