Bird
0
0

You wrote this Angular code:

medium📝 Debug Q6 of 15
Angular - HTTP Client
You wrote this Angular code:
loading = false;
error = '';

fetchData() {
  this.loading = true;
  fetch('api/data')
    .then(() => this.loading = false)
    .catch(() => this.error = 'Error');
}

What is the problem with this code regarding loading state?
ALoading is never set to false on error
BError message is not set on success
CLoading is set to true twice
DFetch URL is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Check loading state changes on success and error

    Loading is set false only in then, not in catch.
  2. Step 2: Identify missing loading reset on error

    On error, loading remains true, causing UI to show loading indefinitely.
  3. Final Answer:

    Loading is never set to false on error -> Option A
  4. Quick Check:

    Always reset loading false on both success and error [OK]
Quick Trick: Set loading false in both then and catch blocks [OK]
Common Mistakes:
MISTAKES
  • Forgetting loading false on error
  • Assuming error sets loading false automatically
  • Ignoring error message setting

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes