Performance: Timeout interceptor
This affects server response time and user interaction speed by limiting how long a request can take before being aborted.
import { timeout } from 'rxjs/operators'; intercept(context, next) { return next.handle().pipe(timeout(5000)); // 5 seconds timeout }
async intercept(context, next) { return next.handle().toPromise(); // no timeout applied }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No timeout interceptor | N/A (server-side) | N/A | N/A | [X] Bad |
| Timeout interceptor with 5s limit | N/A (server-side) | N/A | N/A | [OK] Good |
What is the main purpose of a TimeoutInterceptor in a NestJS application?
Which of the following is the correct way to apply a timeout of 5 seconds using RxJS timeout operator inside a NestJS interceptor?
return next.handle().pipe( ... )timeout operator expects time in milliseconds, so 5 seconds is 5000 ms.Given this TimeoutInterceptor code snippet, what will be the output if the request handler takes 7 seconds to respond?
intercept(context, next) {
return next.handle().pipe(
timeout(5000),
catchError(err => throwError(() => new RequestTimeoutException()))
);
}Identify the error in this TimeoutInterceptor implementation:
intercept(context, next) {
return next.handle().pipe(
timeout(3000),
catchError(err => {
throw new RequestTimeoutException();
})
);
}catchError, throwing an error directly breaks the observable chain; it should return an observable error using throwError.throw inside catchError instead of returning an observable error. is the error.throw inside catchError instead of returning an observable error. -> Option AYou 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?
Pass the test and this pattern moves unknown