Bird
0
0

Given this interceptor code, what will be logged?

medium📝 component behavior Q4 of 15
NestJS - Interceptors
Given this interceptor code, what will be logged?
class LogInterceptor implements NestInterceptor {
  intercept(context, next) {
    console.log('Before handler');
    return next.handle().pipe(tap(() => console.log('After handler')));
  }
}
ABefore handler logged before method, After handler logged after method
BOnly Before handler is logged
COnly After handler is logged
DNo logs appear because tap is not called
Step-by-Step Solution
Solution:
  1. Step 1: Understand intercept method flow

    Console logs 'Before handler' immediately before calling next.handle().
  2. Step 2: Analyze tap operator effect

    Tap runs after the observable completes, logging 'After handler'.
  3. Final Answer:

    Before handler logged before method, After handler logged after method -> Option A
  4. Quick Check:

    Logs before and after method = Before handler logged before method, After handler logged after method [OK]
Quick Trick: tap() runs after observable completes, logging post-handler [OK]
Common Mistakes:
  • Thinking tap runs before next.handle()
  • Assuming no logs because of async
  • Confusing order of logs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes