Bird
0
0

Consider this interceptor code:

medium📝 component behavior Q5 of 15
NestJS - Interceptors
Consider this interceptor code:
intercept(context, next) {
  console.log('Before');
  return next.handle().pipe(tap(() => console.log('After')));
}

What will be logged when a request is handled?
AAfter (immediately), then Before (after response)
BBefore (immediately), then After (after response)
COnly Before
DOnly After
Step-by-Step Solution
Solution:
  1. Step 1: Identify synchronous and asynchronous logs

    'Before' logs immediately before calling next.handle().
  2. Step 2: Understand tap operator timing

    tap logs 'After' when the response stream emits, after the handler completes.
  3. Final Answer:

    Before (immediately), then After (after response) -> Option B
  4. Quick Check:

    tap logs after response, console before next.handle() [OK]
Quick Trick: console before next.handle() runs immediately [OK]
Common Mistakes:
  • Assuming tap runs before next.handle()
  • Thinking both logs run simultaneously
  • Missing asynchronous nature of tap

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes