title: Delay an async method after the NestJS controller method response finish
body: <p>I have a <code>@Get()</code> method in a Controller. I get the response, but after the response is successfully I would like to call an async method after a delay of some milliseconds.</p>
  <p>I'm using a Middleware, but seems like the response is pending for the time specified in the async method.</p>
  <p>How can I do to solve the response in time, and after the delay call the custom method from the Custom Service?</p>
  <p>Here is the used sample code:</p>
  <pre><code>@Controller()
  export class CustomController {
    @Get(&quot;custom-controller-route&quot;)
    getCustomValue(@Res() response: Response) {
      return response.status(200).send({
         value: 1
      })
    }
  }
  </code></pre>
  <p>The middleware code is the following:</p>
  <pre><code>@Injectable()
  export class CustomMiddleware implements NestMiddleware {
    constructor(private readonly customService: CustomService) {}
    use(req: any, res: any, next: () =&gt; void) {
      let send = res.send
      res.send = async (customResponse: CustomResponse) =&gt; {
        const { value } = customResponse
        await customService.customMethod(value, 5000) // Delay of 5 seconds
        res.send = send
        return res.send(exchangeRateResponse)
      }
      next()
    }
  }
  </code></pre>
  <p>And the CustomService has the next code:</p>
  <pre><code>@Injectable()
  export class CustomService {
      async customMethod(value: any, delay) {
        await firstValueFrom(
          timer(delay).pipe(
            tap(() =&gt; {
              // Here is the logic that needs to be run in the delay time after the response is finished
             console.log(`Custom Service - custom method called with: ${value} after: ${delay} milliseconds.`)
            })
          )
        )
      }
  }
  </code></pre>