Created
July 12, 2017 22:54
-
-
Save gskachkov/923acbe6d8813fce2955f9d0dcd0e6c3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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