Bird
0
0

Given this Angular service method:

medium📝 Predict Output Q4 of 15
Angular - HTTP Client
Given this Angular service method:
getData() {
  return this.http.get('/api/data').pipe(
    catchError(err => {
      console.error('Error:', err.status);
      return of([]);
    })
  );
}

What will the component receive if the HTTP request fails?
AAn empty array
BAn error object
CUndefined
DNull
Step-by-Step Solution
Solution:
  1. Step 1: Analyze catchError return value

    The catchError operator returns of([]) which emits an empty array observable on error.
  2. Step 2: Understand observable emission to component

    The component subscribing to this method will receive the empty array instead of an error.
  3. Final Answer:

    An empty array -> Option A
  4. Quick Check:

    catchError returns fallback observable = empty array [OK]
Quick Trick: catchError can return fallback data to avoid errors in components [OK]
Common Mistakes:
MISTAKES
  • Expecting error object instead of fallback data
  • Assuming undefined or null is returned
  • Ignoring catchError fallback return

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes