Skip to content

Instantly share code, notes, and snippets.

@simonjcarr
Created August 15, 2020 15:18
Show Gist options
  • Select an option

  • Save simonjcarr/83f069b91f6c0e2c0fab42ae4cee76fa to your computer and use it in GitHub Desktop.

Select an option

Save simonjcarr/83f069b91f6c0e2c0fab42ae4cee76fa to your computer and use it in GitHub Desktop.
import { ref } from "vue";
export default function usePosts() {
let post = ref({});
let posts = ref([]);
let user = ref({});
function fetchPosts() {
fetch(`https://jsonplaceholder.typicode.com/posts`)
.then(response => response.json())
.then(data => (posts.value = data));
}
function fetchPost(postId) {
fetch(`https://jsonplaceholder.typicode.com/posts/${postId}`)
.then(response => response.json())
.then(data => (post.value = data))
.then(data => fetchUser(data.userId));
}
function fetchUser(userId) {
fetch(`https://jsonplaceholder.typicode.com/users/${userId}`)
.then(response => response.json())
.then(data => (user.value = data));
}
return { fetchPosts, fetchPost, fetchUser, post, posts, user };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment