Cedric Poilly
*
Step | Type |
---|---|
1. Select random user, by id | sync |
2. Fetch all of user's posts | async |
3. Select random post id | sync |
Step | Type |
---|---|
4. Fetch all of post's comments | async |
5. Select random comment id | sync |
6. Fetch selected comment | async |
Step | Type |
---|---|
7. Update comment | sync |
8. Push updated comment (PUT) | async |
9. Fetch updated comment | async |
10. Log updated comment | sync |
function getUserPostComment() {
// ...
fetchData(
`${POSTS_URL}?userId=${userIndex}`,
function success(posts) {
// ...
fetchData(
`${COMMENTS_URL}?postId=${postId}`,
function success(comments) {
// ...
fetchData(
`${COMMENTS_URL}/${commentId}`,
function success() {
// ...
fetchData(
`${COMMENTS_URL}/${commentId}`,
function success(response) {
// ...
},
function error(err) {
console.error(err);
}
)
},
function error(err) {
console.error(err);
},
"PUT",
JSON.stringify(updateComment)
)
},
function error(err) {
console.error(err);
}
)
},
function error(err) {
console.error(err);
}
)
}
getUserPostComment()
function getUserPostComment() {
const userIndex = generateRandomPositiveNumber(0, 10);
console.debug("Step 1: fetch user's posts");
fetchData(
`${POSTS_URL}?userId=${userIndex}`,
fetchPostComment,
error
)
}
function fetchPostComment(posts) {
console.log(`User has ${posts.length} posts.`);
const count = posts.length
const postIndex = generateRandomPositiveNumber(0, count - 1);
const post = posts[postIndex]
const { id: postId } = post
console.debug("Step 2: fetch post's comment.");
fetchData(
`${COMMENTS_URL}?postId=${postId}`,
modifyCommentBody,
error
)
}
function modifyCommentBody(comments) {
const count = comments.length
const commentIndex = generateRandomPositiveNumber(0, count - 1);
const comment = comments[commentIndex]
const { id: commentId } = comment
console.log("Original comment:", comment);
const updateComment = { ...comment, body: "Body was changed!" }
console.debug("Step 3: modify the comment's body.");
fetchData(
`${COMMENTS_URL}/${commentId}`,
fetchUpdatedComment.bind(null, commentId),
error,
"PUT",
JSON.stringify(updateComment)
)
}
function fetchUpdatedComment(commentId) {
console.debug("Step 4: fetch the updated comment.");
fetchData(
`${COMMENTS_URL}/${commentId}`,
logUpdatedComment,
error
)
}
function logUpdatedComment(comment) {
console.log("Comment was updated:", comment);
}
function error(err) {
const { statusText, responseURL } = err
const parts = responseURL.split("/")
const lastButOneIndex = parts.length - 2
const entityPart = parts[lastButOneIndex]
const entity = entityPart.substring(0, entityPart.length - 1)
console.error("The", entity, "was", statusText.toLowerCase(), err);
}
function getUserPostComment() {
const userIndex = generateRandomPositiveNumber(0, 10);
console.debug("Step 1: fetch user's posts");
fetchData(
`${POSTS_URL}?userId=${userIndex}`,
fetchPostComment,
error
)
}
function fetchPostComment(posts) {
console.log(`User has ${posts.length} posts.`);
const count = posts.length
const postIndex = generateRandomPositiveNumber(0, count - 1);
const post = posts[postIndex]
const { id: postId } = post
console.debug("Step 2: fetch post's comment.");
fetchData(
`${COMMENTS_URL}?postId=${postId}`,
modifyCommentBody,
error
)
}
function modifyCommentBody(comments) {
const count = comments.length
const commentIndex = generateRandomPositiveNumber(0, count - 1);
const comment = comments[commentIndex]
const { id: commentId } = comment
console.log("Original comment:", comment);
const updateComment = { ...comment, body: "Body was changed!" }
console.debug("Step 3: modify the comment's body.");
fetchData(
`${COMMENTS_URL}/${commentId}`,
fetchUpdatedComment.bind(null, commentId),
error,
"PUT",
JSON.stringify(updateComment)
)
}
function fetchUpdatedComment(commentId) {
console.debug("Step 4: fetch the updated comment.");
fetchData(
`${COMMENTS_URL}/${commentId}`,
logUpdatedComment,
error
)
}
function logUpdatedComment(comment) {
console.log("Comment was updated:", comment);
}
function error(err) {
const { statusText, responseURL } = err
const parts = responseURL.split("/")
const lastButOneIndex = parts.length - 2
const entityPart = parts[lastButOneIndex]
const entity = entityPart.substring(0, entityPart.length - 1)
console.error("The", entity, "was", statusText.toLowerCase(), err);
}
function getUserPostComment() {
const userIndex = generateRandomPositiveNumber(0, 10);
console.debug("Step 1: fetch user's posts");
fetchData(
`${POSTS_URL}?userId=${userIndex}`,
fetchPostComment,
error
)
}
function fetchPostComment(posts) {
console.log(`User has ${posts.length} posts.`);
const count = posts.length
const postIndex = generateRandomPositiveNumber(0, count - 1);
const post = posts[postIndex]
const { id: postId } = post
console.debug("Step 2: fetch post's comment.");
fetchData(
`${COMMENTS_URL}?postId=${postId}`,
modifyCommentBody,
error
)
}
function modifyCommentBody(comments) {
const count = comments.length
const commentIndex = generateRandomPositiveNumber(0, count - 1);
const comment = comments[commentIndex]
const { id: commentId } = comment
console.log("Original comment:", comment);
const updateComment = { ...comment, body: "Body was changed!" }
console.debug("Step 3: modify the comment's body.");
fetchData(
`${COMMENTS_URL}/${commentId}`,
fetchUpdatedComment.bind(null, commentId),
error,
"PUT",
JSON.stringify(updateComment)
)
}
function fetchUpdatedComment(commentId) {
console.debug("Step 4: fetch the updated comment.");
fetchData(
`${COMMENTS_URL}/${commentId}`,
logUpdatedComment,
error
)
}
function logUpdatedComment(comment) {
console.log("Comment was updated:", comment);
}
function error(err) {
const { statusText, responseURL } = err
const parts = responseURL.split("/")
const lastButOneIndex = parts.length - 2
const entityPart = parts[lastButOneIndex]
const entity = entityPart.substring(0, entityPart.length - 1)
console.error("The", entity, "was", statusText.toLowerCase(), err);
}
getUserPostComment()
function getUserPostComment() {
const userIndex = generateRandomPositiveNumber(0, 10);
console.debug("Step 1: fetch user's posts");
return fetchData(`${POSTS_URL}?userId=${userIndex}`)
}
function fetchPostComment(posts) {
console.log(`User has ${posts.length} posts.`);
const count = posts.length
const postIndex = generateRandomPositiveNumber(0, count - 1);
const post = posts[postIndex]
const { id: postId } = post || {}
console.debug("Step 2: fetch post's comment.");
return fetchData(`${COMMENTS_URL}?postId=${postId}`)
}
function modifyCommentBody(comments) {
const count = comments.length
const commentIndex = generateRandomPositiveNumber(0, count - 1);
const comment = comments[commentIndex]
const { id: commentId } = comment || {}
console.log("Original comment:", comment);
const updateComment = { ...comment, body: "Body was changed!" }
console.debug("Step 3: modify the comment's body.");
return fetchData(
`${COMMENTS_URL}/${commentId}`,
"PUT",
JSON.stringify(updateComment)
)
}
function fetchUpdatedComment({ id: commentId }) {
console.debug("Step 4: fetch the updated comment.");
return fetchData(`${COMMENTS_URL}/${commentId}`)
}
function logUpdatedComment(updatedComment) {
console.log("Comment was updated:", updatedComment);
}
function error(err) {
const { statusText, responseURL } = err
const parts = responseURL.split("/")
const lastButOneIndex = parts.length - 2
const entityPart = parts[lastButOneIndex]
const entity = entityPart.substring(0, entityPart.length - 1)
console.error("The", entity, "was", statusText.toLowerCase(), err);
}
getUserPostComment()
.then(fetchPostComment)
.then(modifyCommentBody)
.then(fetchUpdatedComment)
.then(logUpdatedComment)
.catch(error)
function getUserPostComment() { /* ... */ }
function fetchPostComment(posts) { /* ... */ }
function modifyCommentBody(comments) { /* ... */ }
function fetchUpdatedComment({ id: commentId }) { /* ... */ }
function logUpdatedComment(updatedComment) { /* ... */ }
function error(err) { /* ... */ }
getUserPostComment()
.then(fetchPostComment)
.then(modifyCommentBody)
.then(fetchUpdatedComment)
.then(logUpdatedComment)
.catch(error)
function getUserPostComment() {
// ...
fetchData(`${POSTS_URL}?userId=${userIndex}`)
.then(function fetchPostComment(posts) {
// ...
fetchData(`${COMMENTS_URL}?postId=${postId}`)
.then(function modifyCommentBody(comments) {
// ...
fetchData(
`${COMMENTS_URL}/${commentId}`,
"PUT",
JSON.stringify(updateComment)
)
.then(function fetchUpdatedComment(commentId) {
// ...
fetchData(
`${COMMENTS_URL}/${commentId}`)
.then(function logUpdatedComment(comment) {
console.log("Comment was updated:", comment);
})
.catch(error)
})
.catch(error)
})
.catch(error)
})
.catch(error)
}
function error(err) {
const { statusText, responseURL } = err
const parts = responseURL.split("/")
const lastButOneIndex = parts.length - 2
const entityPart = parts[lastButOneIndex]
const entity = entityPart.substring(0, entityPart.length - 1)
console.error("The", entity, "was", statusText.toLowerCase(), err);
}
getUserPostComment()
By Cedric Poilly