Home > other >  Does the Nestjs controller method have to be async if it returns a promise?
Does the Nestjs controller method have to be async if it returns a promise?

Time:09-30

Very simple general question:

@Controller('something')
class SomeController {
  @Get()
  foobar() {
     return foo() // this returns a promise
  }
}

So in such a case, do I have to make the foobar() controller method async? My understanding is that this is not necessary. NestJS will resolve the returned promise automatically. Making the method async is only needed if I wanna await inside.

Is this correct?

CodePudding user response:

Technically, it's not necessary, Nest will view the promise as is and resolve it before sending the response, but it's generally a good practice to mark your promise returning methods as async, just to stay in the habit and be clear about what it is returning

  • Related