Skip to content

Instantly share code, notes, and snippets.

@gskachkov
Created July 12, 2017 22:54
Show Gist options
  • Save gskachkov/923acbe6d8813fce2955f9d0dcd0e6c3 to your computer and use it in GitHub Desktop.
Save gskachkov/923acbe6d8813fce2955f9d0dcd0e6c3 to your computer and use it in GitHub Desktop.
var apiRoot = "https://jsonplaceholder.typicode.com/";
var currentUserCode = "[email protected]";
function fetchCurrentUser(cb) {
$.ajax({url: apiRoot + "users",
complete: function(result) {
if (result.status === 200) {
var users = result.responseJSON;
var currentUser = users
.filter(user => user.email === currentUserCode)
.pop(currentUser);
cb(currentUser);
} else {
console.log('Error during loading');
}
}
});
}
function fetchUserPosts(userId, cb) {
$.ajax({url: apiRoot + "posts?userId=" + userId,
complete: function(result) {
if (result.status === 200) {
cb(result.responseJSON);
} else {
console.log('Error during loading');
}
}
});
}
function fetchCommentsForPost(postId, cb) {
$.ajax({url: apiRoot + "comments?postId=" + postId,
complete: function(result) {
if (result.status === 200) {
cb(result.responseJSON);
} else {
console.log('Error during loading');
}
}
});
}
function flow() {
fetchCurrentUser(function (currentUser) {
fetchUserPosts(currentUser.id, function (posts) {
for(var i = 0; i < posts.length; i++) {
fetchCommentsForPost(post.id, function (comments) {
console.log(comments);
});
}
});
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment