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.
A. Using throw inside catchError instead of returning an observable error.
B. Timeout value should be in seconds, not milliseconds.
C. Missing import for RequestTimeoutException.
D. The interceptor must return a Promise, not an Observable.
Solution
Step 1: Check error handling inside catchError
Inside catchError, throwing an error directly breaks the observable chain; it should return an observable error using throwError.
Step 2: Validate other options
Timeout is correctly in milliseconds, imports are assumed correct, and interceptors return Observables, so only Using throw inside catchError instead of returning an observable error. is the error.
Final Answer:
Using throw inside catchError instead of returning an observable error. -> Option A
Quick Check:
Use throwError() inside catchError, not throw [OK]
You want to create a TimeoutInterceptor that applies a 3-second timeout globally but allows a specific route to override it with a 10-second timeout. Which approach correctly implements this behavior?
hard
A. Set timeout to 10 seconds globally and manually check route inside interceptor to reduce to 3 seconds if needed.
B. Apply two TimeoutInterceptors globally, one with 3 seconds and one with 10 seconds; the last one wins.
C. Create two separate interceptors and apply the 10-second one only on the specific route without global interceptor.
D. Use a global TimeoutInterceptor with 3 seconds and a custom decorator to set metadata for 10 seconds, then read metadata inside the interceptor to override timeout.
Solution
Step 1: Understand global and route-specific timeout needs
Global interceptor applies 3 seconds by default; specific routes can override by metadata.
Step 2: Implement override using custom decorator and metadata
Custom decorator sets timeout metadata on route; interceptor reads it and uses that value if present.
Step 3: Evaluate other options
Applying multiple global interceptors causes conflicts; setting longer timeout globally and reducing for most routes reverses the logic; creating separate interceptors without a global one loses timeout protection on other routes.
Final Answer:
Use a global TimeoutInterceptor with 3 seconds and a custom decorator to set metadata for 10 seconds, then read metadata inside the interceptor to override timeout. -> Option D
Quick Check:
Use metadata + decorator to override global timeout [OK]
Hint: Use custom decorator + metadata to override global interceptor timeout [OK]
Common Mistakes:
Applying multiple global interceptors causing conflicts
Hardcoding timeout values without metadata
Not reading route metadata inside interceptor
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.