[Vuejs]-Ideal way to chain asynchronous calls with axios

0👍

You first two functions return promices, not actual data. You should try something like this:

async function get_user(value){
  return (await axios.get( "https://apicall/" + value )).data;
}

async function get_user_posts(username){
  return (await axios.get("https://apicall/" + username)).data;
}

Or, if you keep first two functions unchanged:

var UsersOutput = async function () {
  const userProfile = (await get_user(2928928)).data;
  const userPosts = (await get_user_posts(userProfile.data.username)).data;
  return { userProfile, userPosts }
}

I, personally, find the first option better, because your getter functions return objects not promices, so you don’t clutter your code with (await ).data;. But it’s a matter of taste.

-2👍

I think you need to look on this link.
https://javascript.info/async-await

In your case you can use the .then() but I still prefer to use async await

By using await in async function you can chain some different actions and use the last call in your current

Leave a comment