0
0
NestJSframework~5 mins

Timeout interceptor in NestJS - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a Timeout Interceptor in NestJS?
A Timeout Interceptor in NestJS is a piece of code that automatically stops a request if it takes longer than a set time. It helps keep the app responsive by avoiding long waits.
Click to reveal answer
intermediate
How do you apply a Timeout Interceptor globally in NestJS?
You apply it globally by using app.useGlobalInterceptors(new TimeoutInterceptor()) in the main bootstrap file. This makes all requests use the timeout rule.
Click to reveal answer
intermediate
Which RxJS operator is commonly used inside a Timeout Interceptor to enforce the timeout?
The timeout operator from RxJS is used. It throws an error if the observable does not emit a value within the specified time.
Click to reveal answer
beginner
What happens if a request exceeds the timeout set by the Timeout Interceptor?
The interceptor throws a TimeoutError, which usually results in a 408 Request Timeout response sent back to the client.
Click to reveal answer
advanced
Show a simple example of a Timeout Interceptor class in NestJS.
import { Injectable, NestInterceptor, ExecutionContext, CallHandler, RequestTimeoutException } from '@nestjs/common';
import { Observable, throwError, TimeoutError } from 'rxjs';
import { timeout, catchError } from 'rxjs/operators';

@Injectable()
export class TimeoutInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    return next.handle().pipe(
      timeout(5000), // 5 seconds timeout
      catchError(err => {
        if (err instanceof TimeoutError) {
          return throwError(() => new RequestTimeoutException());
        }
        return throwError(() => err);
      }),
    );
  }
}
Click to reveal answer
What is the main purpose of a Timeout Interceptor in NestJS?
ATo authenticate users
BTo stop requests that take too long
CTo cache responses
DTo log all incoming requests
Which RxJS operator is used to enforce a timeout in a NestJS interceptor?
AmergeMap
Bmap
Cfilter
Dtimeout
What exception is typically thrown when a timeout occurs in NestJS?
ABadRequestException
BUnauthorizedException
CRequestTimeoutException
DNotFoundException
How do you apply a Timeout Interceptor to all routes in a NestJS app?
ACall app.useGlobalInterceptors() with the interceptor
BUse @Timeout() decorator on each controller
CAdd timeout in main.ts without interceptor
DSet timeout in the database config
If a Timeout Interceptor is set to 3 seconds, what happens if a request takes 5 seconds?
AThe request is canceled and a timeout error is returned
BThe request completes normally
CThe server crashes
DThe request is retried automatically
Explain how a Timeout Interceptor works in NestJS and why it is useful.
Think about how to handle slow requests in a web app.
You got /4 concepts.
    Describe how to create and apply a Timeout Interceptor globally in a NestJS application.
    Focus on the class structure and global setup.
    You got /4 concepts.