Bird
0
0

Identify the error in this facade service method:

medium📝 Debug Q14 of 15
Angular - Advanced Patterns

Identify the error in this facade service method:

fetchData() {
  this.apiService.getData().subscribe(data => {
    this.data = data;
  });
  return this.data;
}
AUsing arrow function inside subscribe is invalid syntax
BReturns data before subscription completes, causing undefined result
CSubscription should be inside component, not service
DMissing return type annotation causes compile error
Step-by-Step Solution
Solution:
  1. Step 1: Understand asynchronous subscription

    The subscribe callback runs later, so this.data is not set immediately.
  2. Step 2: Identify return timing issue

    The method returns this.data immediately, likely undefined before data arrives.
  3. Final Answer:

    Returns data before subscription completes, causing undefined result -> Option B
  4. Quick Check:

    Async subscribe returns undefined early [OK]
Quick Trick: Return inside subscribe or use Observable return [OK]
Common Mistakes:
  • Returning data before async call finishes
  • Thinking arrow functions are invalid in subscribe
  • Believing subscription must be in component only

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes