Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

343
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda