Donate

Async Fetch Not Working When Retrieving Data From JSONPlaceholder API In JavaScript And Vue.JS

Hello,

I was trying to retrieve data from a Free Fake API website called JSON Placheholder using the code below which using asynchronous behavior. However, this does'nt return data after all.
 async getDataAsync(){ 
       try {
         let response = await fetch('http://jsonplaceholder.typicode.com/todos');
         console.log(response.data);
         this.todos = response.data;
       } catch (error) {
         console.log(error);
    }
After doing some research, I came up with a solution and replace the code above using JavaScript Promise as a means to retrieve data from the Fake API website in an asynchronous manner.
await fetch('http://jsonplaceholder.typicode.com/todos')
            .then(response => 
              response.json()
             )
            .then(data => {
              this.todos = data;
            })
            .catch(error => {
              return Promise.reject(error);
            });
    }
Async Fetch Not Working When Retrieving Data From JSONPlaceholder API In JavaScript And Vue.JS

Cheers!

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

Pass GUID As Parameter To Action Using ASP.NET MVC ContribGrid