The Delightful World of Vue
Smarter Loading: Ajax status as State
  Start your All-Access Pass to unlock this challenge
Buy Access Login

Challenge 1 / 1

In this section, we learned that you can wrap any AJAX call in a try...catch to catch any errors that may arise during that AJAX call. We also say that we should limit that try...catch to just the statement that performs the call.

Here's an example:

async created() {
    this.loading = true;
    let response;

    try {
        response = await axios.get('/api/get-turtle');
    } catch(e) {
        this.loading = false;
        return;
    }

    this.loading = false;
    this.products = response.data;
},

Why is it important that the try...catch only includes the AJAX call?

Skip challenges and go to theNext Chapter

Turn Challenges Off?

All further challenges will be skipped automatically.
You can re-enable challenges at any time on this page or from your account page.