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

134
Visualizações
Express Async Method in Method

I´m quite new to ExpressJS, so i have a question about my Code.

    router.post("/create", (req: Request, res: Response) => {
    pruefungController.create(req, res);
});


public async create(req: Request, res: Response): Promise<void> {
        const pruefung = new Pruefung({
            fach: req.body.fach,
            datum: req.body.datum,
            raum: req.body.raum
        });
        await pruefung.save();
        res.send(pruefung);
    }

Is it enough to declare the create method as async or do i have to declare the callback also as async like this?

router.post("/create", async (req: Request, res: Response) => {
    await pruefungController.create(req, res);
});

In my opinion it should be enough to just await the action of the .save() method, but i don´t know how Express handles the method passed into the router.post() method :(

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The only time you need to declare a function as async is if you use the await keyword inside it.

The only time you need to use the await keyword is if you need a function to wait for a promise to resolve before continuing.

The function you pass to post doesn't do anything after calling pruefungController.create. Nor does anything care about the value it returns. You don't need to use await there so you don't need to make it async.

For that matter, that function doesn't do anything except call another function with the same arguments, so you can get rid of it:

router.post("/create", pruefungController.create);
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